Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Filter by prop
(version: 0)
Filter by prop
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", "64ecabef-4967-4a45-87c4-74de8fe9176e", "ec672725-66fc-45cc-b909-4ef1134715ba", "64248b27-40bf-4464-82b8-40a55ec769db", ] var n = 14600; 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 provided benchmark definition and explain what is being tested. **Benchmark Definition** The benchmark defines four different approaches to filter an array of objects based on a specific property: 1. `// filter, Array.includes`: using the `includes` method of the `Array` class 2. `// filter, Set from array`: creating a set from the array and then filtering it 3. `// filter, Set from loop`: creating a set by iterating over the array and adding each element individually 4. `// filter, in Object`: using the `in` operator to check if an object property exists **Options Compared** The benchmark is comparing the performance of these four approaches on the same dataset. **Pros and Cons** Here's a brief overview of the pros and cons of each approach: 1. `Array.includes`: * Pros: simple, efficient, and widely supported. * Cons: can be slow for large datasets due to its linear search nature. 2. `Set from array`: * Pros: fast lookup times and good cache locality. * Cons: requires more memory overhead due to the set data structure. 3. `Set from loop`: * Pros: allows for more control over the filtering process, can be faster for small datasets. * Cons: slower than other approaches due to the iteration and set creation steps. 4. `in Object`: * Pros: simple and efficient, but may not work as expected for non-object values. * Cons: slower than some other approaches due to the dynamic property lookup. **Library Usage** None of the benchmarking code explicitly uses any external libraries. **Special JS Features/Syntax** The benchmark does use a few special JavaScript features: 1. `Arrow functions` (e.g., `(e) => e.account_id`) 2. `Template literals` (e.g., `"${i}-a".repeat(10)`) These features are widely supported and do not introduce any significant overhead. **Other Alternatives** If the authors of the benchmark wanted to explore alternative approaches, they could consider: 1. Using a more advanced data structure, such as a `Map`, instead of a set. 2. Implementing a custom filtering algorithm that takes advantage of the specific properties of the dataset. 3. Using parallel processing or multi-threading to speed up the filtering process. However, these alternatives may require significant changes to the benchmarking code and may not offer substantial performance gains.
Related benchmarks:
map filter vs reduce
Array.prototype.filter vs Lodash.filter chained
reduce vs filter 22
reduce vs map/filter --- filter and reshape
Array.find vs Array.filter on large dataset
Comments
Confirm delete:
Do you really want to delete benchmark?