Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Bound Symbol.iterator effect on for..of loop performance
(version: 0)
It is believed that v8 optimizes for..of loops in some way. Will this optimization goes away if [Symbol.iterator] is replaced to a bound function?
Comparing performance of:
native set vs set with [Symbol.iterator] modified to a bound function
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var randomArray = []; for (let i = 0; i < 10000; i++) { randomArray[i] = i*i; } var set1 = new Set(randomArray); var set2 = new Set(randomArray); set2[Symbol.iterator] = set2[Symbol.iterator].bind(set2);
Tests:
native set
for (let item of set1) { if (typeof item !== 'number') { throw new Error('wtf'); } }
set with [Symbol.iterator] modified to a bound function
for (let item of set2) { if (typeof item !== 'number') { throw new Error('wtf'); } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
native set
set with [Symbol.iterator] modified to a bound function
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 benchmark test case and explain what is being tested. **Benchmark Description** The test case is designed to measure the performance difference between two approaches: 1. **Native Set**: This approach uses the built-in `Set` object, which automatically handles iteration using its `Symbol.iterator` property. 2. **Modified Set with Bound Symbol.Iterator**: In this approach, the `Symbol.iterator` property of the `Set` object is manually bound to the `Set` instance using the `bind()` method. **What's being tested?** The test case is checking how V8 (the JavaScript engine used by Chrome and other browsers) optimizes for...of loops in sets. Specifically, it's testing whether the optimization goes away if the `Symbol.iterator` property is replaced with a bound function. **Options compared** Two options are being compared: 1. **Native Set**: This approach uses the built-in `Set` object. 2. **Modified Set with Bound Symbol.Iterator**: In this approach, the `Symbol.iterator` property of the `Set` object is manually bound to the `Set` instance using the `bind()` method. **Pros and Cons** * **Native Set**: + Pros: Simple and straightforward implementation. + Cons: Might not be optimized for performance by V8. * **Modified Set with Bound Symbol.Iterator**: + Pros: Forces V8 to use a specific optimization path, which might improve performance. + Cons: Requires manual binding of the `Symbol.iterator` property, adding complexity to the code. **Other considerations** This test case is relevant because it targets a specific optimization in V8. By testing this scenario, the benchmark creators are trying to understand how V8 handles iteration over sets and whether the optimization is robust across different approaches. **Library and Purpose** The `Set` object is a built-in JavaScript library that provides a data structure for storing unique values. Its purpose is to allow efficient lookup, insertion, and removal of elements while maintaining uniqueness. **Special JS Feature or Syntax** This test case uses the `Symbol.iterator` property, which is a syntax feature introduced in ECMAScript 2015 (ES6). It's used to define a custom iterator protocol for objects. In this case, it's being used to iterate over the set using a...of loop. **Other alternatives** There are other ways to implement iteration over sets, such as: * Using a `forEach()` method with a callback function. * Creating an array of values from the set and iterating over that array. * Implementing a custom iterator class. However, these approaches might not be relevant for this specific benchmark test case.
Related benchmarks:
JavaScript spread operator vs Slice/Splice performance - 2
JavaScript spread operator vs Slice/Splice performance testing
JavaScript spread operator vs Slice/Splice performance, passing
JavaScript spread operator vs Slice/Splice performance 2
JavaScript spread operator vs Slice/Splice performance 2edas
Comments
Confirm delete:
Do you really want to delete benchmark?