Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Date.now() vs new Date().getTime() vs cast new Date()
(version: 0)
Comparing performance of:
Date.now() vs new Date().getTime(); vs +new Date();
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var timestamp = null;
Tests:
Date.now()
timestamp = Date.now();
new Date().getTime();
timestamp = new Date().getTime();
+new Date();
timestamp = +new Date();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Date.now()
new Date().getTime();
+new Date();
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36
Browser/OS:
Chrome 127 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Date.now()
7600728.0 Ops/sec
new Date().getTime();
7025969.5 Ops/sec
+new Date();
5529055.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of what is being tested on the provided JSON benchmark. **Benchmark Overview** The benchmark is comparing three different ways to get the current timestamp in JavaScript: 1. `Date.now()` 2. `new Date().getTime()` 3. `+new Date()` (using the unary plus operator) **Options Compared** The test cases are comparing the performance of each option, which means we're looking at how many times each operation can be executed per second. * `Date.now()`: This method returns the number of milliseconds since the Unix Epoch (January 1, 1970, 00:00:00 UTC) as a fixed-point number. It's designed to provide high-resolution timing. * `new Date().getTime()`: This method returns the number of milliseconds since the Unix Epoch for the specified date and time. However, due to its implementation, it can be slower than `Date.now()` because it takes into account the time zone and daylight saving time adjustments. * `+new Date()`: This is a trick that uses the unary plus operator (`+`) to force JavaScript to perform a primitive conversion on the result of calling `new Date()`, effectively casting the result to a number. While this might seem like an optimization, it's actually just syntactic sugar for `Date.now()`. **Pros and Cons** * **`Date.now()`**: Pros: fast, accurate, and widely supported. Cons: assumes a fixed-point representation of time, which can lead to issues with very large or very small timestamps. * **`new Date().getTime()`**: Pros: takes into account time zone and daylight saving time adjustments. Cons: slower than `Date.now()`, as it's more complex and involves more operations. * **`+new Date()`**: Pros: syntactic sugar for `Date.now()`, so it might seem like an optimization. Cons: doesn't provide any actual performance benefits, and its documentation is less clear due to the syntax trickery. **Library and Special JS Features** There are no libraries used in this benchmark, but there's a special JavaScript feature being utilized: * The unary plus operator (`+`) is used to force a primitive conversion on the result of calling `new Date()`. This is not a standard JavaScript feature, but rather an optimization technique. **Other Considerations** This benchmark is focused on measuring performance and comparing different approaches. However, it's essential to note that `Date.now()` should be preferred over `+new Date()` due to its simplicity and readability. The difference in performance between these two options is unlikely to have a significant impact on most use cases. **Other Alternatives** If you need more accurate timing or want to account for time zone adjustments, you might consider using other methods: * For accurate timing: `performance.now()`, which returns the number of milliseconds since the page was loaded (or in older browsers, since the last navigation event). * To account for daylight saving time changes: `Date.now()` with a specific time zone argument (e.g., `new Date().getTimezoneOffset()`) or using a library like moment.js. In summary, this benchmark is testing different ways to get the current timestamp in JavaScript. While `+new Date()` might seem like an optimization, it's actually just syntactic sugar for `Date.now()`, and there are no significant performance differences between these options. The focus of this benchmark should be on highlighting the importance of using `Date.now()` for its simplicity, readability, and accurate timing.
Related benchmarks:
Date.now() vs new Date().getTime()
new Date().getTime() vs Date.now()
Date.now() vs new Date()
Date.now() vs new Date().getTime(qu)
Date.now() - Date.now() vs new Date() - new Date()
Comments
Confirm delete:
Do you really want to delete benchmark?