Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Is String Set be faster than many equal? (3)
(version: 1)
Comparing performance of:
Without Set vs With Set
Created:
2 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script> var n00 = 9000; // sufficient large no. for init var n11 = 400; // no. of rules var n12 = 2000; // no. of items var s01, s02, f01, f02, st; var test01 = function() { const rules = s01; const items = s02; const f = f01; let result = 0; let ui = 0; let vi = 0; for (const item of items) { ui++; if (f(item)) continue; vi++; result += ui; } return `${vi}|${result}`; } var test02 = function() { const rules = s01; const items = s02; const f = f02; let result = 0; let ui = 0; let vi = 0; for (const item of items) { ui++; if (f(item, st)) continue; vi++; result += ui; } return `${vi}|${result}`; } var initialFn = function() { const fullList = [...new Set(new Array(n00).fill(0).map((e, i) => `${i}::${crypto.randomUUID()}`))]; // unique random samples let trialCount = 8; do { if (--trialCount < 0) throw new Error(`incorrect lengths`); const k01 = new Array(n00).fill(0).map((e, i) => Math.random() > 0.5); const k02 = new Array(n00).fill(0).map((e, i) => Math.random() > 0.5); const m01 = fullList.map((e, i) => k01[i] ? e : null).filter(e => e).sort((a, b) => Math.random() - 0.5); const m02 = fullList.map((e, i) => k02[i] ? e : null).filter(e => e).sort((a, b) => Math.random() - 0.5); s01 = m01.slice(0, n11); // 400 rules s02 = m02.slice(0, n12); // 2000 items } while (s01.length < n11 || s02.length < n12); f01 = new Function('key', s01.map(e => `if(key==='${e}')return true;`).join('\n') + 'return false;'); f02 = new Function('key', 's', `if(s.has(key))return true;` + 'return false;'); st = new Set(s01); } </script>
Script Preparation code:
Benchmark.prototype.setup = function() { initialFn(); const rr01 = test01(); const rr02 = test02(); if (rr01 !== rr02) throw new Error(`test01/02 mismatched - ${rr01} & ${rr02}`); console.log('test 01/02', rr01); }; Benchmark.prototype.teardown = function() { s01.length = 0; s02.length = 0; st.clear(); s01 = s02 = f01 = f02 = st = null; }
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:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Zaap3.6.8
Browser/OS:
Chrome 123 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Without Set
134.7 Ops/sec
With Set
10423.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its various components. **Benchmark Overview** The benchmark is designed to compare the performance of two approaches: using a set data structure (`test02`) versus not using a set data structure (`test01`). **Test Cases** There are two individual test cases: 1. **Without Set**: This test case uses the `test01()` function, which iterates over an array of items and checks each item against a set of rules. The iteration is done using a simple loop. 2. **With Set**: This test case uses the `test02()` function, which also iterates over an array of items and checks each item against a set of rules. However, this time, it uses a JavaScript `Set` data structure to efficiently check for membership. **Library and Purpose** In both test cases, the `Set` library is used to create and manipulate sets of data. A `Set` is a collection of unique values that can be added or removed dynamically. In the context of this benchmark, the `Set` is used to store the rules (`s01`) and check for membership. **JavaScript Features and Syntax** There are no special JavaScript features or syntax being tested in this benchmark. However, it's worth noting that the use of arrow functions (`=>`) is not specific to this benchmark, as they have been a part of the ECMAScript language specification since 2015. **Approach Comparison** The two approaches can be compared as follows: * **Without Set**: This approach uses a simple loop to iterate over the items and checks for membership using a conditional statement. The iteration is done using a linear search, which has a time complexity of O(n). * **With Set**: This approach uses the `Set` data structure to efficiently check for membership. The lookup operation in a `Set` has an average time complexity of O(1), making it much faster than the linear search used in the "Without Set" approach. **Pros and Cons** The pros and cons of each approach are: * **Without Set**: + Pros: Simple to implement, no dependencies on external libraries. + Cons: Linear search has a high time complexity, making it slower for large datasets. * **With Set**: + Pros: Efficient lookup operation, much faster than linear search. + Cons: Requires an external library (JavaScript `Set`), may have additional overhead due to the extra dependency. **Other Alternatives** If the JavaScript environment does not support the use of `Set`, other alternatives can be considered: * Using a custom data structure, such as a hash table or binary search tree. * Implementing a more efficient algorithm, such as a Bloom filter or prefix tree. * Using a different programming language that supports sets more efficiently. However, these alternatives may add complexity and overhead to the benchmark, so it's essential to weigh the pros and cons before choosing an alternative approach.
Related benchmarks:
Which equals operator (== vs ===) is faster2?
Which equals operator (== vs ===) is faster? 2
Which equals operator (== vs ===) is faster? 3
large string size comparison
Comments
Confirm delete:
Do you really want to delete benchmark?