Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Date.now() vs new Date().getTime() vs +new Date()
(version: 0)
Comparing performance of:
Date.now() vs new Date().getTime(); vs +new Date()
Created:
5 years 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:
3 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 Edg/144.0.0.0
Browser/OS:
Chrome 144 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Date.now()
2442871.5 Ops/sec
new Date().getTime();
2091735.8 Ops/sec
+new Date()
1760063.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark, hosted on MeasureThat.net, aims to compare the performance of three different methods for obtaining the current timestamp in JavaScript: `Date.now()`, `new Date().getTime()`, and `+new Date()`. **Test Cases** There are three test cases: 1. **`Date.now()`**: This method uses the built-in `Date.now()` function to get the number of milliseconds since the Unix epoch (January 1, 1970). 2. **`new Date().getTime()`**: This method creates a new `Date` object and calls its `getTime()` method to get the timestamp in milliseconds. 3. **`+new Date()`**: This method uses the unary plus operator (`+`) to convert the result of calling `new Date()` to an integer, which is equivalent to getting the timestamp in milliseconds. **Options Compared** The benchmark compares the performance of these three methods across different executions per second (e.g., 9495372.0, 8859815.0, and 6899383.0). **Pros and Cons** * **`Date.now()`**: + Pros: Simple, efficient, and widely supported. + Cons: May not work correctly in older browsers or environments that don't support modern JavaScript features. * **`new Date().getTime()`**: + Pros: More flexible than `Date.now()`, as it allows for more customization of the date object (e.g., setting specific time zones). + Cons: Requires creating a new `Date` object, which can be slower and use more memory than using `Date.now()` directly. * **`+new Date()`**: + Pros: Similar to `new Date().getTime()`, but with less overhead due to the unary plus operator. + Cons: May not work correctly in older browsers or environments that don't support modern JavaScript features. **Library and Special JS Features** There are no libraries mentioned in this benchmark. However, it's worth noting that MeasureThat.net uses a custom implementation for executing JavaScript benchmarks on different browsers and platforms. The benchmark does use modern JavaScript features, such as the `let` keyword ( implicit block scoping) and template literals (string interpolation). **Other Alternatives** If you need to compare timestamp methods in other programming languages or environments, consider using alternative implementations: * For Python: `time.time()` or `datetime.datetime.now()`. * For Java: `System.currentTimeMillis()` or `java.util.Date()`. * For C/C++: `time(NULL)` or `gettimeofday()`. * For Node.js (in addition to JavaScript): `performance.now()`. Keep in mind that the performance characteristics of different timestamp methods may vary depending on the specific use case, platform, and browser.
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?