Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test1111111111111111111111111111111111111
(version: 0)
Comparing performance of:
array from vs for of
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div class="a"> <div class="b"></div> <div class="b c"> </div> </div>
Tests:
array from
var nodes = document.querySelectorAll('.b') var res = {}; Array.from(nodes).filter(x => !x.className.includes('c')).forEach((g, i) => res[i] = g)
for of
var nodes = document.querySelectorAll('.b') var res = {}; let __ctr = 0 for(const node of nodes) { if(node.className.includes('c')) continue; res[__ctr] = node; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array from
for of
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 what's being tested on the provided JSON and explain the different approaches compared. **Script Preparation Code** The Script Preparation Code is empty, which means that no specific setup or initialization code is required for the benchmark tests. This allows test users to run their own scripts alongside the benchmark script. **Options Compared** Two main options are being compared: 1. **Array.from()**: This method creates a new array from an iterable object (in this case, the `nodes` variable). The test case uses `Array.from()` to create an array of elements that match the `.b` selector. 2. **For...of loop**: The other option is using a traditional for loop with a variable (`__ctr`) to iterate over the `nodes` array and populate the `res` object. **Pros and Cons** **Array.from():** Pros: * Concise and efficient way to create arrays from iterables * Can be used in modern browsers (ECMAScript 2015+) Cons: * May not work as expected in older browsers or versions of Chrome **For...of loop:** Pros: * Works consistently across all browsers, including older ones * May be more readable for some developers due to the explicit control over iteration variables Cons: * Can lead to slower performance compared to `Array.from()` due to the overhead of iterating over the array manually * Requires more code and boilerplate **Library** None. The benchmark script uses only native JavaScript features and does not require any external libraries. **Special JS Features/Syntax** There are no special JavaScript features or syntax being tested in these benchmarks, as both options use standard JavaScript constructs (arrays, loops). **Other Considerations** When choosing between `Array.from()` and a traditional for loop, consider the following: * If you need to support older browsers, the for loop might be a better choice. * If you're targeting modern browsers and prioritize conciseness, `Array.from()` is likely a better option. * In terms of performance, both options are comparable in this specific benchmark. However, if you're concerned about performance, using `Array.from()` with a small array size (like 100-1000 elements) might be more efficient due to the optimized implementation. **Alternatives** Other alternatives for creating arrays from iterables include: * `slice()`: Creates a shallow copy of a portion of an array. * `map()`: Applies a transformation function to each element of an array and returns a new array with the results. * `concat()`: Combines two or more arrays into a single array. However, these alternatives might not be as concise or efficient as `Array.from()` for this specific use case.
Related benchmarks:
Jquery fastest selector
Dom benchmark
Selector Order
querySelector vs querySelectorAll simple 2
JS selector functions
Comments
Confirm delete:
Do you really want to delete benchmark?