Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set Iteration with spread
(version: 0)
Comparing performance of:
set foreach vs set spread
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script> window.object = {}; for (var i = 0, length = Math.pow(10, 5); i < length; ++i) object[i] = Math.random(); window.array = Object.getOwnPropertyNames(object).map(function(key) { return object[key]; }); try { window.set = new Set(array); } catch(e){ console.log("Set is not supported"); } try { window.map = new Map(array.map(function(value, i) { return [i, value]; })); } catch(e){ console.log("Map is not supported"); } //chrome bug if (window.set && set.size == 0 || window.map && map.size == 0) array.forEach(function(value, index) { if (window.set) set.add(value); if (window.map) map.set(index, value); }); window.method = function(value, index, container) { value; index; container; } </script>
Tests:
set foreach
set.forEach(method);
set spread
[...set].forEach(method);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
set foreach
set spread
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **What is tested?** The benchmark measures the performance of two approaches to iterate over a `Set` object in JavaScript: 1. `set.forEach(method)`: This approach uses the `forEach` method, which is a built-in method for iterating over sets. 2. `[...set].forEach(method)`: This approach uses the spread operator (`[...]`) to convert the `Set` object to an array, and then iterates over it using `forEach`. **Options compared** The benchmark compares the performance of these two approaches: * **Approach 1: `set.forEach(method)`** + Pros: - Native implementation, likely to be optimized for performance. - May have better cache locality due to iteration over a set's underlying data structure. + Cons: - Might not work in older browsers or environments that don't support `forEach` on sets. * **Approach 2: `[...set].forEach(method)`** + Pros: - Works in older browsers and environments that don't support `forEach` on sets (due to polyfills or fallbacks). - Can be used with other arrays, as the spread operator can convert any iterable object to an array. + Cons: - May have slower performance due to the overhead of creating an intermediate array. - May have poorer cache locality compared to the native `forEach` method. **Other considerations** * **Browser support**: The benchmark is run on Chrome 81, a relatively modern browser. However, if running this benchmark on older browsers or environments, the results might be affected by differences in implementation or lack of support for certain features. * **JavaScript engine optimization**: The JavaScript engine (V8) used in Chrome 81 likely optimizes the native `forEach` method and the spread operator approach for performance. * **Library usage**: The benchmark uses `Object.getOwnPropertyNames` to get an array of object property keys, which is a built-in method. It also uses `Map` and `Set`, which are also built-in data structures in modern browsers. **Special JS feature or syntax** There are no special JavaScript features or syntaxes used in this benchmark that require specific handling or considerations. **Alternatives** If you wanted to compare the performance of these approaches on other browsers, you could modify the benchmark to use different browsers. Alternatively, you could try using polyfills or fallbacks for older browsers to see how they affect performance. Some possible alternatives for comparing different iteration methods include: * Using `Array.prototype.forEach` instead of `set.forEach` * Using a library like Lodash's `forEachIn` function * Using a custom implementation of the iteration method
Related benchmarks:
Array range generating
Set Iteration with spread 2
Array from() vs Map.keys() vs Map.values() vs spread (fixed)
Object vs Map lookup: random integer key
Comments
Confirm delete:
Do you really want to delete benchmark?