Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs some for arrays of objects
(version: 0)
Comparing performance of:
Set vs Some
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = new Array(1000).fill({ field1: Math.ceil(Math.random() * 1000) }); var arr2 = new Array(1000).fill({ field2: Math.ceil(Math.random() * 1000) });
Tests:
Set
const set = new Set(arr.map((el) => el.field1)); const result = arr2.filter(el => set.has(el.field2));
Some
const result = arr2.filter(el => arr.some((i) => el.field2 === i.field1));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Set
Some
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
84882.6 Ops/sec
Some
906.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what is being tested. **Benchmark Definition** The benchmark definition represents two test cases: 1. `Set`: This test case creates a new Set object from an array of objects using the `map` method. It then filters another array of objects based on the presence of a certain field (`field2`) in the set. 2. `Some`: This test case uses the `some` method to filter an array of objects based on whether a certain field (`field1`) exists in another array. **Options being compared** In this benchmark, two options are being compared: * `Set`: Using a Set object to store unique values and then filtering the array using the `has` method. * `Some`: Using the `some` method with a callback function that checks for the existence of a value in another array. **Pros and Cons** Here's a brief analysis of each approach: * **Set**: + Pros: Efficient use of memory, fast lookup times, and good performance for large datasets. + Cons: May be slower than other methods for small datasets due to the overhead of creating and maintaining a Set object. * `Some`: + Pros: Simple and straightforward implementation, suitable for small to medium-sized datasets. + Cons: May not perform well for very large datasets or datasets with many duplicate values. **Library** In this benchmark, no external libraries are used. However, the `Set` data structure is a built-in JavaScript object that provides efficient set operations. **Special JS feature or syntax** The only special feature used in this benchmark is the use of `map`, which creates a new array by applying a transformation function to each element in the original array. **Other alternatives** If you wanted to write an alternative implementation for one of these test cases, here are some options: * For the `Set` test case: + Using `JSON.parse(JSON.stringify(arr))` and then using `in` operator to check for presence (although this is slower than a Set object). + Using a custom data structure, such as a hash table or a binary search tree. * For the `Some` test case: + Using `every` method with a callback function that checks for the existence of a value in another array. + Using a library like Lodash or Underscore.js that provides more efficient iteration and filtering methods. Keep in mind that these alternative implementations may not provide the same level of performance as the original implementation, and may require additional overhead or memory usage.
Related benchmarks:
at 500 vs [500]
Array fill method vs for loop
Array push or set
new Array() vs Array.from() with random data
Array.from VS spreading for
Comments
Confirm delete:
Do you really want to delete benchmark?