Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Can add to WeakSet ?
(version: 0)
How to check beforehand that the value won't throw exception when added to WeakSet in JavaScript? What is the most performant way?
Comparing performance of:
Switch-Case 2 vs !== null && (typeof || typeof)
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var DATA = (function (K, N, total) { function random(n) { return Math.floor(Math.random() * 7); } function randomKey() { return Math.random().toString(36).slice(2); } function randomCollection(count = random(N)) { for (var a = new Array(count), i = 0; i < count; i++) { a[i] = randomValue(); } return a; } function randomObject(keysCount = random(N)) { for (var obj = {}, i = 0; i < keysCount; i++) { obj[randomKey()] = randomValue(); } return obj; } function randomValue(type = random(K)) { switch (type % K) { case 0: return undefined; case 1: return null; break; case 2: return String(Math.random()); case 3: return Math.random() > 0.5; case 4: return Math.random() * 1E3; case 5: return randomCollection(); default: return randomObject(); } } return randomCollection(total); }(7, 5, 1E6)); function canBeAddedToWeakSet_switch2(value) { switch (typeof value) { case 'undefined': return String(value) === '[object HTMLAllCollection]' case 'boolean': case 'number': case 'string': case 'symbol': return false case 'object': return value !== null default: return true } } function canBeAddedToWeakSet_null_typeof_x2(value) { return value !== null && (typeof value === 'object' || typeof value === 'function'); } function test(canAdd) { const count = DATA.reduce((countOfAddables, value) => (canAdd(value) ? 1 : 0) + countOfAddables, 0); console.log('can add %d out of %d values in the array', count, DATA.length); }
Tests:
Switch-Case 2
test(canBeAddedToWeakSet_switch2);
!== null && (typeof || typeof)
test(canBeAddedToWeakSet_null_typeof_x2);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Switch-Case 2
!== null && (typeof || typeof)
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 provided benchmark definition and explain what's being tested. **Benchmark Definition** The benchmark defines two test cases: 1. `canBeAddedToWeakSet_switch2`: This function takes a value as input and returns `true` if it can be added to a WeakSet using a switch-case statement. 2. `canBeAddedToWeakSet_null_typeof_x2`: This function takes a value as input and returns `true` if it can be added to a WeakSet by checking if the value is not null and either an object or a function. **Options Compared** The two test cases compare different approaches to check if a value can be added to a WeakSet: * `canBeAddedToWeakSet_switch2`: Uses a switch-case statement to check the type of the value. * `canBeAddedToWeakSet_null_typeof_x2`: Uses a combination of the null and typeof operators to check the type of the value. **Pros and Cons** Here are some pros and cons of each approach: 1. `canBeAddedToWeakSet_switch2`: * Pros: Simple and easy to understand, can be optimized for specific types (e.g., using a lookup table). * Cons: May not work correctly for certain edge cases or values with complex types. 2. `canBeAddedToWeakSet_null_typeof_x2`: * Pros: More general-purpose and handles more edge cases than the switch-case approach. * Cons: Less efficient, as it involves multiple operations (null check, typeof operator). **Libraries and Special JS Features** There is no explicit library used in this benchmark definition. However, the `WeakSet` data structure is a built-in JavaScript feature. **Other Considerations** When working with WeakSets, it's essential to consider the following: * WeakSets are designed for storing unique values without ensuring any particular order or structure. * When adding values to a WeakSet, the value must be non-null and not already present in the set. * The `typeof` operator can return unexpected results for certain types (e.g., symbols). **Alternatives** Other approaches to check if a value can be added to a WeakSet might include: * Using a lookup table or optimization technique specific to the expected input types. * Utilizing other JavaScript data structures, such as Sets or Maps, which may provide similar functionality with different performance characteristics. For this specific benchmark, the two test cases `canBeAddedToWeakSet_switch2` and `canBeAddedToWeakSet_null_typeof_x2` provide a clear comparison between different approaches to handling type checking in WeakSets.
Related benchmarks:
array vs int16array try catch
Map vs Object 5000 rand
Includes vs Find (Javascript)
map.has vs. array.includes - large array - random Access
Object.keys.includes vs object.hasOwnProperty
Comments
Confirm delete:
Do you really want to delete benchmark?