Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set has vs add
(version: 0)
Comparing performance of:
add vs has
Created:
3 years ago
by:
Registered User
Jump to the latest result
Tests:
add
const foo = new Set(); foo.add("a") foo.add("b") foo.add("c") foo.add("d") foo.add("e") foo.add("f") foo.add("f") foo.add("f") foo.add("f") foo.add("f") foo.add("f") foo.add("f")
has
const foo = new Set(); foo.add("a") foo.add("b") foo.add("c") foo.add("d") foo.add("e") foo.add("f") foo.has("f") foo.has("f") foo.has("f") foo.has("f") foo.has("f") foo.has("f")
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
add
has
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/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
add
10614620.0 Ops/sec
has
14340780.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON represents two test cases, each comparing different approaches for interacting with Sets in JavaScript. **Benchmark Definition** The Benchmark Definition JSON specifies that no script preparation code or HTML preparation code are required. **Test Cases** ### Test Case 1: "Set has vs add" This test case compares the performance of checking if an element exists in a Set (`foo.has("f")`) versus adding multiple elements to the Set and then checking for existence (`foo.add("f"), foo.add("f"), ...`). The JavaScript code is: ```javascript const foo = new Set(); foo.add("a") foo.add("b") foo.add("c") foo.add("d") foo.add("e") foo.add("f") foo.add("f") foo.add("f") foo.add("f") foo.add("f") foo.add("f") foo.add("f") ``` The test is repeated several times to ensure consistent results. ### Test Case 2: "Set has vs add" This test case is similar to the previous one, but with a different approach: The JavaScript code is: ```javascript const foo = new Set(); foo.add("a") foo.add("b") foo.add("c") foo.add("d") foo.add("e") foo.add("f") foo.has("f") foo.has("f") foo.has("f") foo.has("f") foo.has("f") foo.has("f") ``` **Options Compared** * **Checking for existence (`foo.has("f")`)** vs. **Adding multiple elements and then checking for existence (`foo.add("f"), foo.add("f"), ...`)** **Pros and Cons:** * **Checking for existence**: + Pros: Less code to execute, potentially faster due to fewer operations. + Cons: May require more memory access or iterations, depending on the Set implementation. * **Adding multiple elements and then checking for existence**: + Pros: Can be faster due to the use of caching mechanisms in modern JavaScript engines. + Cons: Requires more code to execute, potentially slower due to the additional operations. In this specific test case, the performance difference between these two approaches is relatively small. However, in other scenarios, one approach may be more efficient than the other. **Library:** No external libraries are used in either of these test cases. **Special JS Feature/ Syntax:** None of the provided test cases use any special JavaScript features or syntax beyond basic Set operations. **Alternative Approaches:** * **Using a different data structure**: Instead of using Sets, you could compare performance with other data structures like Arrays or Linked Lists. * **Adding multiple elements in a single operation**: You could modify the code to add all elements at once using `foo.add("a", "b", ..., "f")`, which might be more efficient due to caching mechanisms. Overall, this benchmark provides a simple yet informative comparison of two common use cases for Sets in JavaScript.
Related benchmarks:
convert to set + set.has vs. array.includes
new Set([x]).has vs [x].includes
array.includes vs. set.has on the fly
Array includes vs Set.has
set.has (w/ creation) vs. array.includes
Comments
Confirm delete:
Do you really want to delete benchmark?