Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs strict equality
(version: 0)
Comparing performance of:
Set vs Strict equality
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var set = new Set([`foo`, `bar`, `baz`, `qux`]); var values = [`a`, `foo`, `bar`, `b`, `c`]; var count = 0;
Tests:
Set
for (const x of values) if (set.has(x)) count += 1;
Strict equality
for (const x of values) if (x === `foo` || x === `bar` || x === `baz` || x === `qux`) count += 1;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Set
Strict equality
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):
I'll break down the benchmark and its test cases, explaining what's being tested, compared, and considering pros and cons. **Benchmark Definition** The benchmark definition provides a basic setup for the tests: ```json "Script Preparation Code": "var set = new Set([`foo`, `bar`, `baz`, `qux`]);\r\nvar values = [`a`, `foo`, `bar`, `b`, `c`];" ``` This code creates a new `Set` object containing the strings `"foo"`, `"bar"`, `"baz"`, and `"qux"`. It also defines an array `values` with mixed case strings. **Test Cases** There are two test cases: 1. **Set**: This test uses the `has()` method of the `Set` object to check if a value exists in the set. ```json "Benchmark Definition": "for (const x of values) if (set.has(x)) count += 1;" ``` The benchmark iterates over the `values` array and increments a counter for each value that is present in the `set`. 2. **Strict equality**: This test uses the traditional `===` operator to check for exact string matches. ```json "Benchmark Definition": "for (const x of values) if (x === `foo` || x === `bar` || x === `baz` || x === `qux`) count += 1;" ``` The benchmark iterates over the `values` array and increments a counter for each value that is exactly equal to one of the strings in the `set`. **Pros and Cons** * **Set**: + Pros: Efficient lookup, good for large datasets. + Cons: May not be suitable for exact string matching due to case sensitivity. Requires a `Set` object creation, which may incur overhead. * **Strict equality**: + Pros: Simple, easy to understand, and flexible (works with strings of varying lengths). + Cons: Less efficient than `Set` lookup, especially for large datasets. **Library** The `Set` object is a built-in JavaScript library that provides an efficient way to store and look up unique values. It's implemented in the browser's V8 engine. **Special JS feature or syntax** There are no special JavaScript features or syntax used in this benchmark. The tests rely solely on standard JavaScript language constructs. **Other Alternatives** If you wanted to rewrite the `Set` lookup using a different approach, you could consider: * Using an object with specific keys instead of a `Set`. * Utilizing the `indexOf()` method to search for values. * Implementing a custom data structure, like a hash table or trie. However, these alternatives would likely be less efficient and more complex than the original `Set` approach.
Related benchmarks:
set vs array iteration
Includes (array) vs Has (Set)
Small n set vs array
set vs array find if exists
set vs array find if exists v2
Comments
Confirm delete:
Do you really want to delete benchmark?