Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
[#1] Spread vs Array.filter.call
(version: 2)
Comparing performance of:
Spread vs Array.prototype case
Created:
3 years ago
by:
Registered User
Jump to the latest result
Tests:
Spread
const elementCount = [...document.querySelectorAll('*')].filter(node => node.nodeType === node.ELEMENT_NODE);
Array.prototype case
const elementCount = Array.prototype.filter.call(document.querySelectorAll('*'), node => node.nodeType === node.ELEMENT_NODE);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Spread
Array.prototype case
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:147.0) Gecko/20100101 Firefox/147.0
Browser/OS:
Firefox 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Spread
636727.9 Ops/sec
Array.prototype case
657887.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmarking setup. **Benchmark Purpose** The test measures the performance difference between two approaches: using the spread operator (`...`) to create an array from `document.querySelectorAll('*')`, and using the `Array.prototype.filter.call()` method to filter elements. **Options Compared** There are two main options being compared: 1. **Spread Operator**: This approach creates a new array by spreading the NodeList returned by `document.querySelectorAll('*')`. The spread operator is a shorthand way of creating a new array from an iterable. 2. **Array.prototype.filter.call()**: This approach uses the `filter()` method on the Array prototype, passing a callback function to filter elements based on their node type. **Pros and Cons** - **Spread Operator**: * Pros: More concise and readable code. * Cons: Creates a new array, which can be memory-intensive for large datasets. Also, it's not explicitly typed, making it less predictable. - **Array.prototype.filter.call()**: * Pros: Can be more efficient in terms of memory usage, as it doesn't create a new array. * Cons: Requires using the `call()` method to pass the callback function, which can make the code less readable. **Library and Special JS Features** There is no explicit library mentioned in the benchmark definition or test cases. However, `document.querySelectorAll('*')` returns an HTML NodeList, which is a proprietary feature of the Web API for working with HTML documents. **Other Alternatives** If you need to filter elements from a large dataset, other alternatives could be: * Using `Array.from()` instead of the spread operator. * Using a library like Lodash, which provides a `filterByType` function that can handle node types and reduce memory usage. * Using a different data structure, such as a Set or Map, if the filtering logic is complex. **Benchmark Preparation Code** The benchmark preparation code is empty in this case. In a typical benchmarking setup, you would create a scenario where the code to be measured is executed multiple times to gather performance metrics. The individual test cases are well-defined and provide clear instructions for each approach being tested.
Related benchmarks:
Array .push() vs .unshift() vs spread
Array clone from index 1 to end: spread operator vs slice
Array.prototype.slice vs spread operator on a bigger array
Array.from() vs spread []
Array .push() vs spread operator
Comments
Confirm delete:
Do you really want to delete benchmark?