Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Date.now() vs Performance.measure()
(version: 2)
Performance measure can be slow
Comparing performance of:
performance.mark vs Date.now vs performance.mark + performance.measure
Created:
9 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var times = [];
Tests:
performance.mark
var i = Math.random() + '' + Date.now(); performance.mark(i + 'start'); performance.mark(i + 'end' );
Date.now
var i = Math.random() + '' + Date.now(); times.push(Date.now() + 'start' + i); times.push(Date.now() + 'end' + i);
performance.mark + performance.measure
var i = Math.random() + '' + Date.now(); performance.mark(i + 'start'); performance.mark(i + 'end' ); performance.measure(i, i + 'start', i + 'end')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
performance.mark
Date.now
performance.mark + performance.measure
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:133.0) Gecko/20100101 Firefox/133.0
Browser/OS:
Firefox 133 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
performance.mark
243036.6 Ops/sec
Date.now
1280633.6 Ops/sec
performance.mark + performance.measure
146643.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **What is tested?** The provided benchmark tests three different approaches to measure execution time in JavaScript: 1. `Date.now()`: This approach uses the built-in `Date` object to get the current timestamp, which represents the number of milliseconds since January 1, 1970, 00:00:00 UTC. 2. `performance.mark()`: This approach uses the Web Performance API, specifically the `performance.mark()` function, to create a snapshot of the execution time. It returns a unique token that can be used to measure the execution time later on. 3. `performance.mark() + performance.measure()`: This approach combines both `performance.mark()` and `performance.measure()` functions to measure the execution time. **Options compared** The three approaches are compared in terms of their: * **Accuracy**: How close is the measured execution time to the actual execution time? * **Speed**: How fast is each approach when executing the benchmark? * **Complexity**: How much code and setup is required for each approach? **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. `Date.now()`: * Pros: Simple, widely supported, and relatively fast. * Cons: May not be accurate due to clock skew or other factors that can affect the timestamp. 2. `performance.mark()`: * Pros: More accurate than `Date.now()` since it measures a specific execution time interval. * Cons: Requires Web Performance API support, which may not be available in older browsers. 3. `performance.mark() + performance.measure()`: * Pros: Combines the accuracy of `performance.mark()` with the speed of `performance.measure()`. * Cons: Adds an extra step to the benchmarking process, which can introduce overhead. **Libraries and special features** In this benchmark, no specific libraries are used beyond the built-in Web Performance API. However, it's worth noting that other libraries or frameworks may affect the performance or accuracy of these approaches. **Special JavaScript feature or syntax** The `performance` object is a part of the Web Performance API, which provides tools for measuring and analyzing web page performance. The `mark()` and `measure()` functions are part of this API. These features are specific to modern browsers that support the Web Performance API. **Other alternatives** Some alternative approaches to measuring execution time in JavaScript include: * Using a timer function (e.g., `setInterval()`) with a fixed interval. * Utilizing a library like Benchmark.js or jsperf for benchmarking. * Implementing a custom timing mechanism using web workers, canvas, or other techniques. Keep in mind that each alternative approach has its pros and cons, and the choice of method depends on the specific use case, performance requirements, and target audience.
Related benchmarks:
Date.now() vs new Date().getTime() vs performance.now()
Date.now() vs performance.now()324234
Date.now() vs new Date() vs performance.now()
Date.now() vs new Date().getTime() vs performance.now() with calc
Date.now() vs. now()
Comments
Confirm delete:
Do you really want to delete benchmark?