Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
date time memory test
(version: 1)
Comparing performance of:
Date.now vs new Date().getTime()
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
Date.now
const a = Date.now();
new Date().getTime()
let a = new Date().getTime();
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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Date.now
33581756.0 Ops/sec
new Date().getTime()
28600462.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark provided focuses on measuring the performance of different methods for retrieving the current time in JavaScript. Specifically, it compares two approaches: 1. **`Date.now()`** 2. **`new Date().getTime()`** ### Options Compared 1. **`Date.now()`**: - **Description**: This static method of the Date object returns the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC (the epoch). - **Pros**: - Faster and more efficient since it directly provides the time without needing to create a Date object. - Simpler to read and use for time retrieval. - **Cons**: - Limited to milliseconds; if you need more complex date/time manipulation, you'd have to create a Date object anyway. 2. **`new Date().getTime()`**: - **Description**: This method involves first instantiating a new Date object and then calling the `getTime()` method on that object to retrieve the time in milliseconds since the epoch. - **Pros**: - Can be more versatile when you need additional Date functionality because creating the Date object provides access to other methods related to date manipulation. - **Cons**: - Slower than `Date.now()` due to the overhead of creating a Date object. It involves more memory allocation and processing, which can impact performance, especially if executed frequently. ### Benchmarking Results The latest benchmark results show the following executions per second: - **`Date.now()`**: 33,581,756 executions per second - **`new Date().getTime()`**: 28,600,462 executions per second From these results, we can infer that `Date.now()` is significantly faster than `new Date().getTime()`, making it the better choice if your primary goal is to simply get the current timestamp with minimal overhead. ### Other Considerations - **Use Case**: If the primary task is logging timestamps or intervals (like performance measurements), `Date.now()` is preferable due to its speed. However, if the application requires more extensive date manipulation afterward, it may be reasonable to use `new Date()` once, followed by calls to its methods for more complex operations. - **Browser Variability**: Performance can vary across different browsers or devices, so it is essential to conduct benchmarks relevant to your target audience. However, since the benchmark results were obtained on the same browser and operating system (Chrome 131 on macOS), they provide a consistent comparison. ### Alternatives - **Performance.now()**: If microsecond precision is required, `performance.now()` can be used. This method returns a timestamp measured in milliseconds with up to microsecond precision and is particularly useful for performance measurements. However, it’s more specialized for performance timing rather than simple time retrieval. - **Day.js / Moment.js**: These libraries can provide more powerful date manipulation capabilities, but they come with their performance trade-offs due to abstraction and overhead associated with library dependencies. They're excellent for applications with extensive date manipulations but should be weighed against their performance implications. In summary, when simply measuring the current time, `Date.now()` offers better performance, while `new Date().getTime()` provides more flexibility for additional date-related functionalities. Each option and its associated libraries and methods should always be evaluated based on the specific performance and functionality needs of the application being developed.
Related benchmarks:
Date test
Date vs date time
Date.now vs new Date
Date.now() vs + new Date() vs new Date.getTime()
Date object
Date.now()
getTime() vs now()
Date.now() vs new Date().getTime() vs + new Date
Date.now() vs +new Date() test
date comp
Comments
Confirm delete:
Do you really want to delete benchmark?