Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Iterate-Set-many
(version: 0)
Iterate over a set.
Comparing performance of:
for of: many items vs convert to array: many items vs native iterator: many items vs native iterator w/ size: many items:
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var manyItemSet = new Set(Array.from({ length: 1000 }, (_, i) => i)); var arr = Array.from(manyItemSet);
Tests:
for of: many items
let result = 0; for (const item of manyItemSet) { result += item; }
convert to array: many items
let result = 0; let l = arr.length; for (let i = l; i--;) { result += arr[i]; }
native iterator: many items
let result = 0; let val = null; const iter = manyItemSet.values(); while (!(val = iter.next()).done) { result += val.value; }
native iterator w/ size: many items:
let result = 0; let iter = manyItemSet.values(); for (let i = manyItemSet.size; i--;) { result += iter.next().value; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for of: many items
convert to array: many items
native iterator: many items
native iterator w/ size: many items:
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 and its test cases. **Benchmark Definition** The benchmark is defined by the following JSON object: ```json { "Name": "Iterate-Set-many", "Description": "Iterate over a set.", "Script Preparation Code": "var manyItemSet = new Set(Array.from({ length: 1000 }, (_, i) => i));\r\nvar arr = Array.from(manyItemSet);" } ``` Here, we have a simple benchmark that creates a large set (`manyItemSet`) with 1,000 items and then converts it to an array (`arr`). The script preparation code is executed before the test cases. **Test Cases** The benchmark consists of four test cases: ```json [ { "Benchmark Definition": "let result = 0;\r\nfor (const item of manyItemSet) {\r\n result += item;\r\n}\r\n", "Test Name": "for of: many items" }, { "Benchmark Definition": "let result = 0;\r\nlet l = arr.length;\r\nfor (let i = l; i--;) {\r\n result += arr[i];\r\n}", "Test Name": "convert to array: many items" }, { "Benchmark Definition": "let result = 0;\r\nlet val = null;\r\nconst iter = manyItemSet.values();\r\nwhile (!(val = iter.next()).done) {\r\n result += val.value;\r\n}\r\n", "Test Name": "native iterator: many items" }, { "Benchmark Definition": "let result = 0;\r\nlet iter = manyItemSet.values();\r\nfor (let i = manyItemSet.size; i--;) {\r\n result += iter.next().value;\r\n}", "Test Name": "native iterator w/ size: many items:" } ] ``` These test cases compare different approaches to iterate over the set: 1. `for of` loop 2. Manual array indexing (`arr[i]`) 3. Using a native iterator (`manyItemSet.values()` and `iter.next()`) 4. Using a native iterator with the size (`manyItemSet.size`) **Pros and Cons** Here's a brief overview of each approach: 1. **For of loop**: Pros: concise, easy to read, and efficient (since it's optimized for iteration). Cons: may have overhead due to type checking and garbage collection. 2. **Manual array indexing**: Pros: simple and straightforward. Cons: prone to errors, slow (especially for large arrays), and not as efficient as `for of` or native iterators. 3. **Native iterator**: Pros: efficient, readable, and allows for better control over iteration. Cons: may require more boilerplate code. 4. **Native iterator with size**: Pros: similar to the native iterator, but with an additional optimization (using the size). Cons: slightly less efficient than using `manyItemSet.size` directly. **Library** The benchmark uses the built-in JavaScript `Set` and `Array` objects, as well as the `values()` method of the `Set` interface. **Special JS feature or syntax** There are no special features or syntaxes mentioned in this benchmark. All code is standard JavaScript. **Other alternatives** If you wanted to use alternative libraries or approaches, here are some options: * Use a library like Lodash or Ramda for iteration and array manipulation. * Use a different data structure, such as an array or linked list. * Implement your own custom iterator or loop. However, in this specific benchmark, the JavaScript `Set` and `Array` objects provide a good balance of efficiency, readability, and performance.
Related benchmarks:
array last element big data
Array loop: foreach vs map
Unique Array
test1235161321
for vs forEach vs for..in vs for..of (with fixed iterator item reference)
Comments
Confirm delete:
Do you really want to delete benchmark?