Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Object add
(version: 0)
Comparing performance of:
Object vs Set
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var set = new Set(); var object = {}; var array = new Array(5000).fill().map((_, i) => i);
Tests:
Object
for (let i = 0; i < array.length; i++) { object[i] = true; }
Set
for (let i = 0; i < array.length; i++) { set.add(i); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object
Set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object
3326.9 Ops/sec
Set
2579.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this JavaScript microbenchmark, also known as MeasureThat.net. **Benchmark Definition** The benchmark definition is a JSON object that contains metadata about the test case: * `Name`: The name of the benchmark, which is "Set vs Object add". * `Description`: An empty string, indicating no description for this benchmark. * `Script Preparation Code`: + Creates an empty Set instance (`var set = new Set();`) + Creates an empty object (`var object = {};`) + Initializes an array with 5000 elements and fills it with zeros (`new Array(5000).fill().map((_, i) => i);`) * `Html Preparation Code`: An empty string, indicating no HTML preparation code is required. **Individual Test Cases** The benchmark defines two test cases: 1. **Object** * The `Benchmark Definition` uses a traditional JavaScript `for` loop to iterate over the array elements and assign a value (`true`) to each object property. 2. **Set** * The `Benchmark Definition` uses another `for` loop, but this time with a Set instance (`set.add(i);`). This is where the variation between the two test cases comes in. **Options Compared** Here's what we're comparing: * Two approaches for adding values to an object/element: + Using a traditional JavaScript `Object` (the Object test case). + Using a Set data structure (the Set test case). **Pros and Cons of Each Approach** ### Traditional JavaScript Object (`Object`) Pros: * Familiarity: Most developers are comfortable working with objects in JavaScript. * Easy to understand: The code is straightforward, making it easier for others to review or debug. Cons: * Performance overhead: Objects have more overhead compared to Sets due to the additional memory required to store property names and values. * Slower lookup times: Object lookups can be slower than Set lookups because of the need to traverse a key-value map. ### Set Data Structure (`Set`) Pros: * Faster performance: Sets are designed for fast membership testing and insertion, making them suitable for this type of benchmark. * Smaller memory footprint: Sets use less memory than objects because they don't store property names or values. Cons: * Less familiar: Some developers might be less comfortable working with Set instances, which can make the code harder to understand. * More complex: The `add` method on a Set is used instead of the more traditional object assignment syntax (`object[i] = true;`). **Library and Purpose** There is no specific library mentioned in this benchmark. However, some popular Set libraries for JavaScript are: * Lodash's `Set` module (for utility functions like `setDifference`, `unionBy`, etc.) * Underscore.js's `Set` function (for functional programming utilities) These libraries provide additional functionality on top of the native `Set` data structure, but they're not required for this benchmark. **Special JS Feature or Syntax** There is no special JavaScript feature or syntax used in this benchmark. The code only uses standard JavaScript features and syntax. **Alternatives** Other alternatives for adding values to an object/element include: * Using a Map (which provides fast lookup times and can be more efficient than objects for large datasets) * Using a custom data structure, like a binary search tree or a hash table * Optimizing the code using techniques like memoization, caching, or parallel processing Keep in mind that these alternatives might not be relevant to this specific benchmark, which is focused on comparing the performance of two simple approaches (using an object vs. a Set).
Related benchmarks:
Object spread vs New map
Object spread vs New map with string keys
Object spread vs New map entries
test_spread_vs-map
Comments
Confirm delete:
Do you really want to delete benchmark?