Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs string equals
(version: 0)
Comparing performance of:
fn1 vs fn2
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var keys = new Set([ 'a', 'b', 'c', 'd', 'e', 'f' ]) var fn1 = (key) => keys.has(key) var fn2 = (key) => key === 'a' || key === 'b' || key === 'c' || key === 'd' || key === 'e' || key === 'f'
Tests:
fn1
fn1('e') fn1('z')
fn2
fn2('e') fn2('z')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
fn1
fn2
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
fn1
7273925.5 Ops/sec
fn2
14079640.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Definition** The benchmark measures the performance difference between two JavaScript functions: * `fn1`: uses the `Set` data structure to check if an element exists, using the `has()` method. * `fn2`: uses a simple string equality check with multiple `||` conditions to check if a key matches one of the predefined values. **Options Compared** The benchmark compares two approaches: 1. **Using a Set**: This approach uses the `Set` data structure to store unique elements, allowing for efficient lookups using the `has()` method. 2. **String Equality Check**: This approach uses a simple string equality check with multiple `||` conditions to check if a key matches one of the predefined values. **Pros and Cons** * **Using a Set**: + Pros: Efficient lookups, O(1) time complexity on average. + Cons: Requires additional memory for storing the set, may not be suitable for large datasets. * **String Equality Check**: + Pros: Simple to implement, no additional memory required. + Cons: Linear time complexity (O(n)), may lead to slower performance due to repeated string comparisons. **Library and Purpose** In this benchmark, the `Set` data structure is used. A Set in JavaScript is a collection of unique values, which allows for efficient lookups using the `has()` method. **Special JS Feature or Syntax** There are no special JS features or syntaxes being tested in this benchmark. The focus is on comparing two different approaches to achieve string equality checks. **Other Alternatives** Alternative approaches could include: * Using regular expressions (regex) for string matching * Using a library like Lodash's `isEqual()` function, which provides a more concise way of checking for equality between values * Using a caching mechanism to store the results of previous lookups Overall, this benchmark is designed to help developers understand the performance implications of using a Set vs. a simple string equality check in JavaScript.
Related benchmarks:
Compare check existing on Object / Set / Map
set.has(key) vs. Boolean(obj[key])
set.has vs. Object key lookup for real without bang bang
set.has vs. Object key
Comments
Confirm delete:
Do you really want to delete benchmark?