Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lookup tests
(version: 0)
Comparing performance of:
includes vs lookup vs foreach vs object
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var b = new Set(a) var c = {1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true, 9: true, 10: true}
Tests:
includes
return a.includes(9)
lookup
return b.has(9)
foreach
a.forEach(element => { if(element === 9)return true; }); return false;
object
Boolean(c[9])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
includes
lookup
foreach
object
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):
Let's break down the benchmark and explain what's being tested. **Script Preparation Code** The script preparation code sets up two variables: `a` and `b`. `a` is an array of numbers from 1 to 10, while `b` is a new Set object created from the elements of `a`. The variable `c` is an object with properties corresponding to the elements in `a`, where each property is assigned a value of `true`. **Html Preparation Code** There is no HTML preparation code provided. **Test Cases** The benchmark consists of four test cases, each defining a specific JavaScript expression: 1. **includes**: `return a.includes(9)` 2. **lookup**: `return b.has(9)` 3. **foreach**: `a.forEach(element => { if(element === 9)return true; }); return false;` 4. **object**: `Boolean(c[9])` Let's analyze each test case: * **includes** and **lookup**: These two tests are testing the same scenario, but with different implementations: + `includes` checks if a specific element (in this case, 9) is present in the array `a`. + `lookup` uses the `has` method on the Set object `b`, which also checks for the presence of an element. Both methods are essentially performing a linear search or using a hash table lookup. The results may vary depending on the browser's implementation and the specifics of the input data. * **foreach**: This test uses a traditional iterative approach to check if the number 9 is present in the array `a`. It iterates over each element in the array, checking if it matches 9, and returns true as soon as it finds a match. The loop continues until it exhausts all elements in the array. * **object**: This test uses the property access syntax to check if the value of the key '9' exists in the object `c`. Since the object contains a single property with the key '9', this test is essentially checking for the existence of that property. **Browser and Device Variations** The benchmark results show variations in performance across different browsers, devices, and operating systems. The raw UAS (User Agent String) field indicates the specific browser, device platform, and operating system being used to run each test. The `ExecutionsPerSecond` value represents the number of executions performed per second. **Pros and Cons of Different Approaches** Here's a brief analysis of the pros and cons of each approach: * **includes**: Pros: simple and widely supported; Cons: can be slower than using Set or object lookups. * **lookup**: Pros: optimized for Sets and objects; Cons: may not be as widely supported, especially in older browsers. * **foreach**: Pros: flexible and easy to understand; Cons: may be slower due to the iterative approach. * **object**: Pros: concise and readable; Cons: may rely on specific object syntax and properties. **Other Considerations** * **Set lookups**: The `lookup` test case uses a Set, which provides fast membership testing. However, not all browsers support Sets or have optimized implementations for them. * **Object property access**: The `object` test case relies on the property access syntax, which may not be supported in older browsers. **Alternatives** Other alternatives for similar tests could include: * Using a library like Lodash or Underscore.js to provide utility functions for array and object manipulation. * Implementing custom lookup algorithms, such as binary search or hash tables, for specific use cases. * Using browser-specific features, such as WebAssembly or WebGL, to optimize performance. Keep in mind that these alternatives may not be necessary, depending on the specific requirements of your benchmark or application.
Related benchmarks:
set.has vs. Object key lookup 2
set.has vs. Object key lookup for real
set.has vs. Object key lookup for real without bang bang
set.has vs. Object key
set.has vs. Object key lookup2
Comments
Confirm delete:
Do you really want to delete benchmark?