const date = new Date("2020-03-20 0:04:02");
for (let i = 0; i < 100; i++) {
console.log(date.getDate())
}
for (let i = 0; i < 100; i++) {
console.log(new Date("2020-03-20 0:04:02").getDate())
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
From variable | |
From new Date |
Test name | Executions per second |
---|---|
From variable | 2285.2 Ops/sec |
From new Date | 2080.7 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Definition JSON Explanation
The provided JSON represents a benchmark definition for measuring the performance of accessing the date component of a Date
object in JavaScript. The "Name" field specifies the name of the benchmark, which is "new Date("2020-03-20 0:04:02").getDate()"
.
Script Preparation Code and Html Preparation Code
The "Script Preparation Code" and "Html Preparation Code" fields are empty, indicating that no additional setup or initialization code needs to be executed before running the benchmark. This suggests that the benchmark is designed to measure the performance of a specific JavaScript expression in isolation.
Individual Test Cases
There are two test cases:
date
to access the date component, whereas the original benchmark definition only creates a new Date
object and then accesses its date component using the getDate()
method.getDate()
method on the newly created Date
object without assigning it to any variable.Library and Purpose
None of the provided benchmarks use external libraries or frameworks, suggesting that the performance measurement is focused solely on the JavaScript engine itself.
Special JS Features and Syntax
Neither of the test cases uses any special JavaScript features or syntax. The benchmark definitions are straightforward and only involve basic arithmetic operations and function calls.
Pros and Cons of Different Approaches
The two approaches differ in how they access the date component:
Date
object is already being used elsewhere, and it's more efficient to reuse the existing instance instead of creating a new one.Date
object is only used to access its date component. Creating a new object might be beneficial if the resulting expression is expected to be executed frequently.However, both approaches have similar execution patterns and may not capture any significant performance differences between them.
Other Considerations
MeasureThat.net's benchmarking results can help identify performance issues or bottlenecks in JavaScript engines. The provided benchmarks measure the time it takes for each test case to execute 100 times, which might be a suitable range for such microbenchmarks.
Other alternatives to MeasureThat.net include: