Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
setTimeout vs wrapped setTimeout
(version: 0)
Comparing performance of:
setTimeout vs setTimeout2
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var i = 0; var setTimeout2 = function(callback, timeout) { if (!document.hidden) return window.setTimeout(callback, timeout); }
Tests:
setTimeout
setTimeout(function(){i++},0);
setTimeout2
setTimeout2(function(){i++},0);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
setTimeout
setTimeout2
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 18_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/134.0.6998.33 Mobile/15E148 Safari/604.1
Browser/OS:
Chrome Mobile iOS 134 on iOS 18.3.1
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
setTimeout
2923072.5 Ops/sec
setTimeout2
2457723.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested, compared, and discussed. **What is being tested?** The benchmark compares two approaches to implementing `setTimeout`: 1. The standard `setTimeout` function from the browser's JavaScript engine (e.g., V8 in Chrome). 2. A custom implementation of `setTimeout`, denoted as `setTimeout2`. **Options compared:** * **Document hidden check**: Both implementations check if the document is hidden before invoking the callback, but the custom implementation (`setTimeout2`) also checks for other conditions to avoid potential issues with certain browser versions or extensions. * **Window setTimeout**: The standard implementation uses the native `window.setTimeout` function, while the custom implementation wraps this function. **Pros and Cons:** * **Standard implementation (setTimeout)**: + Pros: - Native implementation, likely optimized for performance. - Supports a wide range of browsers and versions. + Cons: - May have issues with certain browser extensions or plugins that manipulate the `hidden` property. - Less control over the underlying behavior. * **Custom implementation (setTimeout2)**: + Pros: - Provides more control over the behavior, allowing for customization of edge cases. - Can mitigate potential issues with browser extensions or plugins. + Cons: - May be less optimized for performance compared to the native implementation. - Requires additional code and maintenance. **Library:** There is no explicitly mentioned library used in this benchmark. However, it's likely that the custom implementation (`setTimeout2`) uses a utility function to wrap the native `window.setTimeout` function. **Special JS features or syntax:** The benchmark does not use any special JavaScript features or syntax, such as async/await, generators, or decorators. The code is straightforward and focuses on the comparison between two implementations of `setTimeout`. **Other alternatives:** If you want to test alternative approaches to implementing `setTimeout`, you could consider: * Using a different library or framework that provides its own implementation of `setTimeout`. * Implementing `setTimeout` from scratch using low-level JavaScript APIs. * Testing the performance of different scheduling algorithms, such as Coalesced Scheduling or Fixed-Time Queuing. Keep in mind that these alternatives would require significant changes to the benchmark code and may introduce additional complexities.
Related benchmarks:
raw cost of requestAnimationFrame
raw cost of requestAnimationFrame 2
Date.now vs setTimeout
SetTimeout 0 vs nothing
Comments
Confirm delete:
Do you really want to delete benchmark?