Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Date.now() vs new Date().getTime()1
(version: 0)
Comparing performance of:
Date.now() vs new Date().getTime();
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var timestamp = null;
Tests:
Date.now()
timestamp = Date()
new Date().getTime();
timestamp = new Date().toString()
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:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Date.now()
1489216.9 Ops/sec
new Date().getTime();
1460280.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark is designed to compare two ways of obtaining the current timestamp in JavaScript: `Date.now()` and `new Date().getTime()`. **Script Preparation Code** The script preparation code for both benchmarks is identical: ```javascript var timestamp = null; ``` This line sets a variable named `timestamp` to `null`, which will be used to store the result of each benchmark. This is likely a precautionary measure to ensure that the test starts with an empty value, as we'll see later. **Benchmark Definition** The first benchmark definition is: ```javascript "timestamp = Date()" ``` This line uses the built-in `Date` constructor without calling it explicitly, relying on its implicit behavior. The `Date()` function returns a new `Date` object in JavaScript. The second benchmark definition is: ```javascript "timestamp = new Date().toString()" ``` This line creates a new `Date` object using the `new Date()` syntax and then calls its `toString()` method to get a string representation of the timestamp. However, since we're only interested in the numeric value, this approach might not be as efficient. **Test Cases** The two test cases are: 1. **"Date.now()"** This benchmark uses the built-in `Date.now()` function, which returns the number of milliseconds elapsed since the Unix epoch (January 1, 1970, 00:00:00 UTC). 2. **"new Date().getTime();"** This benchmark uses the `new Date()` syntax and then calls its `getTime()` method to get a numeric value representing the timestamp. **Library and Special JavaScript Features** In this benchmark, no libraries or special JavaScript features are used beyond what's built into the language. **Pros and Cons of Each Approach** Here are some pros and cons for each approach: * **`Date.now()`** + Pros: - Fastest execution - Most efficient + Cons: None notable in this case * **`new Date().getTime();`** + Pros: None + Cons: - Slightly slower execution compared to `Date.now()` - More overhead due to calling a method on the `Date` object **Other Alternatives** If you were considering alternative approaches, here are a few options: * **`Date.prototype.valueOf()`**: This method returns the primitive value of the date object. While it's not as efficient as `Date.now()`, it might be faster than `new Date().getTime();`. * **`Intl.DateTimeFormat`.valueOf()`**: This approach uses an Internationalization API to get the timestamp. However, this method is more complex and less well-supported across browsers. * **`performance.now()`**: Some modern JavaScript environments, like WebAssembly, provide a `performance.now()` function that returns the time in nanoseconds since the Unix epoch. However, this function might not be available or compatible with all browsers. In summary, the benchmark is designed to compare two simple ways of getting the current timestamp in JavaScript: using the built-in `Date.now()` function and calling methods on a newly created `Date` object. The results highlight the efficiency benefits of using native functions like `Date.now()`.
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?