Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Date.now() vs new Date()
(version: 0)
Comparing performance of:
Date.now() vs new Date().getTime();
Created:
4 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();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Date.now()
new Date().getTime();
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Date.now()
4852982.0 Ops/sec
new Date().getTime();
4176163.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided benchmark. **What is being tested?** The benchmark compares two approaches to get the current timestamp: `Date.now()` and `new Date().getTime()`. Both methods are designed to return the number of milliseconds since the Unix epoch (January 1, 1970) or the date and time offset from midnight January 1, 1970. **Options being compared** The two options being compared are: 1. `Date.now()`: This method returns the timestamp in milliseconds since the last call to `Date.now()` or the initialization of the `Date` object. 2. `new Date().getTime()`: This method creates a new `Date` object and then calls its `getTime()` method to get the timestamp. **Pros and Cons** * **`Date.now()`**: + Pros: Generally faster, as it doesn't involve creating a new `Date` object. + Cons: May not be supported in older browsers or environments that don't support modern JavaScript features. Additionally, if called repeatedly without calling `reset()`, it can accumulate errors and return incorrect results. * **`new Date().getTime()`**: + Pros: More explicit and easier to understand, as it creates a new date object with the current time. + Cons: Generally slower than `Date.now()` due to the overhead of creating a new `Date` object. **Other considerations** * In modern JavaScript, both methods are supported by most browsers. However, older browsers or environments might not support one or the other. * The `reset()` method is available on some implementations of `Date.now()`, but it's not widely supported. **Library and special JS feature used** There is no specific library mentioned in the benchmark definition or test cases. However, some modern JavaScript engines, like V8 (used by Google Chrome), have their own optimizations for these methods, which might affect the results. * `Date.now()` uses a specialized V8 implementation that returns the timestamp directly from the V8 allocator. * `new Date().getTime()` creates a new date object and then calls its `getTime()` method to get the timestamp. **Special JS feature used** There is no special JavaScript feature explicitly mentioned in the benchmark definition or test cases. However, modern JavaScript engines might employ various optimizations or heuristics that could affect the results, such as: * Just-In-Time (JIT) compilation * Garbage collection optimization Keep in mind that these optimizations are typically invisible to developers and are handled by the engine's internal implementation. **Alternatives** If you want to measure the performance of different timestamp methods or explore alternative approaches, here are some alternatives: 1. **`performance.now()`**: This method returns the number of fractional milliseconds since the performance measurement started. 2. **`Date.parse()`**: This method parses a string representing a date and returns the timestamp in milliseconds. 3. **`Web Workers`**: You can use Web Workers to run your benchmarking code in parallel, which can help measure the performance difference between methods. For more information on these alternatives or other optimization techniques, I recommend exploring the relevant documentation for each method or engine.
Related benchmarks:
Date.now() vs new Date().getTime()
new Date().getTime() vs Date.now()
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?