Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Object
(version: 0)
Comparing performance of:
Has Object vs Has Set
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = ['ab', 'cd', 'ef', 'gh']; var s = new Set(a); var d = {}; a.forEach(val => { d[val] = true; });
Tests:
Has Object
var i = d['ab'] i = d['gi']
Has Set
var i = s.has('ab') i = s.has('gi')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Has Object
Has Set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 18_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Mobile/15E148 Safari/604.1
Browser/OS:
Mobile Safari 18 on iOS 18.3.1
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Has Object
487664064.0 Ops/sec
Has Set
552629696.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its components. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmark, specifically testing the performance of two approaches to check if an element exists in either an object or a set data structure. The benchmark is designed to compare the execution speed of these two methods: accessing an object property using bracket notation (`d['ab']`) and checking if an element exists in a set using the `has` method (`s.has('ab')`). **Script Preparation Code** The script preparation code initializes two variables: 1. `a`: an array containing four strings. 2. `s`: a new Set object created from the `a` array. 3. `d`: an empty object. The code then uses the `forEach` method to iterate over the `a` array and assign a value of `true` to each property in the `d` object that corresponds to an element in the `s` set. This is done for demonstration purposes, as it's not actually necessary for the benchmark test itself. **Html Preparation Code** There is no HTML preparation code provided, which means the benchmark is designed to be run in a headless environment (e.g., Node.js) rather than a browser. **Test Cases** The benchmark consists of two individual test cases: 1. **Has Object**: The script definition checks if an element exists in the `d` object using bracket notation (`d['ab']`) and assigns its value to a variable `i`. It then checks again for another element (`d['gi']`) and assigns its value to another variable `i`. 2. **Has Set**: Similar to the previous test case, but uses the `has` method of the `s` set object to check if an element exists. **Library and Special JS Feature** In this benchmark, no libraries are explicitly mentioned, and there are no special JavaScript features or syntax used beyond what is commonly available in modern JavaScript implementations. However, it's worth noting that using sets (implemented as `Set` objects) and bracket notation (`d['ab']`) are relatively modern features introduced in ECMAScript 2015 (ES6). **Pros and Cons of Different Approaches** Here's a brief summary: 1. **Object Property Access (`d['ab']`)**: * Pros: Wide browser support, likely to be faster due to the native access pattern. * Cons: May not perform well for very large sets or objects, as it involves a more complex lookup process. 2. **Set `has` Method**: * Pros: Faster and more efficient for large sets, as it uses an optimized data structure. * Cons: Less widely supported in older browsers, may require additional setup or polyfills. Other Alternatives If you're interested in exploring alternative approaches or optimization techniques, consider the following: 1. **Using `in` operator**: Instead of bracket notation (`d['ab']`), use the `in` operator to check if an element exists in the object. 2. **Caching results**: If the elements being checked are likely to be present in both the set and object (e.g., due to a shared data source), consider caching the results to avoid repeated lookups. 3. **Native Array Methods**: Instead of using `forEach` or bracket notation, explore native array methods like `includes()` for checking presence. Keep in mind that these alternatives may not necessarily offer significant performance improvements or changes to the benchmark's overall outcome.
Related benchmarks:
set vs array
fromArray or desctucturing to convert Set to array
set vs array iteration
Lodash union() vs native Set() for unique primitives
set vs array find if exists
Comments
Confirm delete:
Do you really want to delete benchmark?