Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Date.now() without floor vs static now() function
(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; var _timer = 1000000; function now() { if (_timer !== undefined) { return _timer; } _timer = 1000000; return _timer; }
Tests:
Date.now()
timestamp = Date.now() / 1000;
new Date().getTime();
timestamp = now();
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/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Date.now()
6938289.5 Ops/sec
new Date().getTime();
5433350.0 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. **Overview** The benchmark compares two approaches to get the current timestamp in JavaScript: 1. `Date.now()` with optional floor filtering 2. A custom `now` function that returns the last set timer value or resets it if necessary 3. Another approach using `new Date().getTime()` **Options being compared** * The first option, `Date.now()`, is a built-in JavaScript method that returns the number of milliseconds since the Unix epoch (January 1, 1970, 00:00:00 UTC) as an integer. * The second option, a custom `now` function, introduces some variability and can be seen as a way to simulate the behavior of `Date.now()` but with more control over the value returned. * The third option, `new Date().getTime()`, is another method to get the current timestamp. However, it returns a number representing milliseconds since the Unix epoch, which may not be suitable for all use cases. **Pros and Cons** 1. **`Date.now()`** * Pros: + Fastest and most straightforward way to get the current timestamp. + Less prone to errors due to fewer manual steps involved. * Cons: + May return a non-integer value if there are millisecond-level precision issues. 2. **Custom `now` function** * Pros: + Allows for more control over the returned value, potentially improving performance or reducing variability in calculations. * Cons: + Adds an extra layer of complexity due to manual handling of timer values and potential errors. 3. **`new Date().getTime()`** * Pros: + May offer better precision than `Date.now()`, although this depends on the specific use case. * Cons: + Returns a number representing milliseconds since the Unix epoch, which may not be suitable for all applications. **Library and special features** There is no library used in these benchmarks. However, some browsers may have additional methods or features that can affect performance, such as: * `Date.now()` on Node.js or Web Workers might behave differently due to specific implementations. * Some browsers may use a different timing model or accumulate timers differently. **Special JS feature** The test case uses the `now` function variable, which is not a built-in JavaScript feature. This custom variable allows for better control over the timer value and can help demonstrate how performance varies with different approaches to getting the current timestamp. **Alternative benchmarks** Other approaches you might consider benchmarking: * Using `Date.now()` with optional floor filtering: `timestamp = Math.floor(Date.now() / 1000)` * Using a more precise timing method, such as `performance.now()` (supported by modern browsers) * Using a custom timer library or framework that may offer better performance for specific use cases Keep in mind that the best approach will depend on your specific requirements and constraints.
Related benchmarks:
new Date().getTime() vs Date.now()
Date.now() vs new Date()
Date.now() - Date.now() vs new Date() - new Date()
Date.now() vs static now() function
Comments
Confirm delete:
Do you really want to delete benchmark?