Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Filter by prop smol
(version: 0)
Filter by prop smol
Comparing performance of:
// filter, Array.includes vs // filter, Set from array vs // filter, Set from loop vs // filter, in Object vs // bucket, Obj
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arrIds = [ "720d8d01-b354-4bf0-85dc-601a5856e54e", "63b12fb8-25b0-4860-97b5-1ef671395028", ] var n = 300; var things = [...Array(n)].map((_, i) => ({ account_id: Math.random() < 0.5 ? `${i}-a`.repeat(10) : arrIds[i % arrIds.length], v: i, })); function bucket(arr, iteratee) { return arr.reduce((acc, v) => { const k = iteratee(v); if (acc[k]) { acc[k].push(v); } else { acc[k] = [v]; } return acc; }, {}) } var iteratee = (e) => e.account_id;
Tests:
// filter, Array.includes
// filter, Array.includes const r1 = things.filter(e => arrIds.includes(e.account_id));
// filter, Set from array
// filter, Set from array const setIds = new Set(arrIds); const r2 = things.filter(e => setIds.has(e.account_id));
// filter, Set from loop
// filter, Set from loop const setIdsLoop = new Set(); for (let i = 0, len = arrIds.length; i < len; ++i) { setIdsLoop.add(arrIds[i]) } const r3 = things.filter(e => setIdsLoop.has(e.account_id));
// filter, in Object
// filter, in Object const objIds = {}; for (let i = 0, len = arrIds.length; i < len; ++i) { objIds[arrIds[i]] = true; } const r4 = things.filter(e => e.account_id in objIds);
// bucket, Obj
// bucket, Obj const bucketedObj = bucket(things, iteratee); let r5; for (let i = 0, len = arrIds.length; i < len; ++i) { if (bucketedObj[arrIds[i]]) { if (r5) { const toAdd = bucketedObj[arrIds[i]]; const existingLength = r5.length; const addedLength = toAdd.length; r5.length = existingLength + addedLength; for (let i = 0; i < addedLength; ++i) { r5[existingLength + i] = toAdd[i] } } else { r5 = bucketedObj[arrIds[i]]; } } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
// filter, Array.includes
// filter, Set from array
// filter, Set from loop
// filter, in Object
// bucket, Obj
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 explain what's being tested. **Benchmark Overview** The benchmark is designed to measure the performance of different ways to filter an array of objects in JavaScript. The filtering methods are compared using the same input data, which consists of an array `things` with 300 elements each having a unique `account_id`. **Filtering Methods** Four filtering methods are being tested: 1. **Array Includes**: Using the `includes()` method on the `arrIds` array to check if an element exists in it. 2. **Set from Array**: Creating a Set from the `arrIds` array and using its `has()` method to check if an element exists in it. 3. **Set from Loop**: Creating a Set by looping through the `arrIds` array and adding each element to it, then using its `has()` method to check if an element exists in it. 4. **In Object**: Using an object with keys equal to the `account_id`s and checking if the element is present as a key in that object. **Comparison of Filtering Methods** Each filtering method has its pros and cons: * **Array Includes**: This method is simple and efficient but may have poor performance for large datasets due to the overhead of searching through the array. It's also less accurate than other methods, as it doesn't handle cases where `account_id` might be a substring or prefix. * Pros: Simple, easy to understand * Cons: Poor performance, inaccurate results * **Set from Array**: Creating a Set from the array allows for efficient lookup and is more accurate than `includes()`. However, it may require additional memory allocation for the Set data structure. * Pros: Efficient, accurate results * Cons: Requires extra memory, can be slower due to Set creation time * **Set from Loop**: This method creates a Set by looping through the array and adding each element to it. It's less efficient than using an existing Set but provides the same accuracy as other methods. * Pros: Less memory-intensive, still accurate results * Cons: Inefficient due to loop overhead * **In Object**: Using an object with keys equal to `account_id`s is a creative solution that avoids creating additional data structures. However, it may have poor performance for large datasets and can lead to slower results. * Pros: Less memory-intensive, unique approach * Cons: Poor performance, potential issues with key collisions **Other Considerations** When choosing an efficient filtering method, consider the trade-off between accuracy, memory usage, and performance. In this case, **Set from Array** provides a good balance between efficiency and accuracy. **Test Results** The test results show that each filtering method has different performance characteristics across various browsers and devices. The top performer is **Set from Array**, which demonstrates its efficiency and accuracy in filtering the array. By understanding the pros and cons of each filtering method, developers can choose the most suitable approach for their specific use case and optimize their code for better performance.
Related benchmarks:
Set vs Filter for unique 40k
Set vs Filter for unique with 10000 items
filterMap vs reduce
filter map vs reduce vs for of vs for in vs for length
Filtering test LZ
Comments
Confirm delete:
Do you really want to delete benchmark?