Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Is String Set be faster than many equal?
(version: 0)
Comparing performance of:
Without Set vs With Set
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var fullList = new Array(9000).fill(0).map((e,i)=>`${i}::${crypto.randomUUID()}`); var k01 = new Array(9000).fill(0).map((e,i)=>Math.random()>0.5); var k02 = new Array(9000).fill(0).map((e,i)=>Math.random()>0.5); var s01 = fullList.map((e,i)=>k01[i]?e:null).filter(e=>e); var s02 = fullList.map((e,i)=>k02[i]?e:null).filter(e=>e); var f01 = new Function('key', s01.map(e=>`if(key==='${e}')return true;`).join('\n')+'return false;'); var f02 = new Function('key','s', `if(s.has(key))return true;`+'return false;'); var st = new Set(s01); var test01 = function(){ const main = s01; const loop = s02; let result = 0; let ui = 0; for(let s of loop){ ui++; if(f01(s))continue; result += ui; } return result; } var test02 = function(){ const main = s01; const loop = s02; let result = 0; let ui = 0; for(let s of loop){ ui++; if(f02(s, st))continue; result += ui; } return result; } console.log('test01',test01()); console.log('test02',test02());
Tests:
Without Set
test01();
With Set
test02();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Without Set
With Set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Without Set
9.2 Ops/sec
With Set
2546.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what is being tested in this JavaScript microbenchmark. **Benchmark Test Cases** The test cases are designed to compare the performance of two approaches: 1. `test01`: This function uses a regular array (`s01`) and filters it using `Array.prototype.filter()`. It then creates a closure (`f01`) that checks if each element is in the original array, and if not, adds a counter increment. 2. `test02`: This function uses a regular array (`s02`) and filters it using `Array.prototype.filter()`. However, instead of creating a closure like in `test01`, it uses the `Set` data structure to store the filtered elements. It then creates another closure (`f02`) that checks if an element is present in the `Set`. **Comparison** The goal of this benchmark is to compare which approach performs better: * Without using a `Set`: Does `test01` outperform `test02`? * With using a `Set`: Does `test02` outperform `test01`? **Pros and Cons** Here are some pros and cons for each approach: Without Set (`test01`): Pros: * Simple implementation * No additional data structure overhead Cons: * Potential performance bottleneck due to repeated array searches in the closure * May be less efficient than using a `Set` for large datasets With Set (`test02`): Pros: * Fast lookup times for elements in the `Set` * Can lead to better performance for large datasets Cons: * Additional data structure overhead (memory usage) * Requires an extra step to create and maintain the `Set` **Library Usage** In this benchmark, the `crypto` library is used for generating random UUIDs. The `Array.prototype.filter()` method is also used, which is a built-in JavaScript method. **Special JS Feature/Syntax** This benchmark does not use any special JavaScript features or syntax that would require specific knowledge to understand. **Alternatives** If you want to write your own implementation of this benchmark, here are some alternatives: * Use a different data structure, such as an object or a Map, instead of an array. * Implement the filtering and closure creation logic using a loop or recursion instead of arrays and closures. * Test with a different number of elements in the arrays or use a different distribution for the random numbers. Keep in mind that rewriting this benchmark will likely have a significant impact on its performance characteristics.
Related benchmarks:
Inserting and Searching in Map vs Object
.find() vs direct access in object by id
set.has vs. array.includes - 1000 UUIDs
Map vs Object UUID Key/Value
Comments
Confirm delete:
Do you really want to delete benchmark?