Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
js inarray
(version: 0)
Comparing performance of:
In vs includes vs A
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var A = { "X1": "X", "X2": "X", "X3": "X", "X4": "X", "X5": "X", "X6": "X", "X7": "X", "X8": "X", "X9": "X", "X10": "X" }; var B = [ "X1", "X2", "X3", "X4", "X5", "X6", "X7", "X8", "X9", "X10" ];
Tests:
In
("X5" in A)
includes
B.includes("X5");
A
A["X5"]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
In
includes
A
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
In
6704071.5 Ops/sec
includes
5947961.0 Ops/sec
A
6501256.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark is designed to measure the performance of JavaScript strings checks. It tests three different approaches: 1. Using an array-like object (`A`) with string properties (e.g., `"X5" in A`) 2. Using the `includes()` method on an array (`B`) (e.g., `B.includes("X5")`) 3. Directly accessing a property of an object (`A["X5"]`) **Options Compared** * `in` vs. `includes()`: These two methods are used to check if a string is present in an array or array-like object. The main difference between them is that `in` checks for the presence of the value as a property name, whereas `includes()` checks for the presence of the value itself. * Array-like object (`A`) vs. Direct access (`A["X5"]`): These two approaches differ in how they access and check the string properties. **Pros and Cons** 1. **in** (using array-like object): * Pros: + Works with arrays as well as array-like objects. + Can be faster for large arrays since it avoids creating a new array or iterating over elements. * Cons: + May require more memory to store the array-like object, especially if it's large. + Can be slower for small arrays since it needs to iterate over elements to find the property name. 2. `includes()` (using an array): * Pros: + Fast and efficient for both large and small arrays. + Avoids iterating over elements, making it suitable for arrays with many null or undefined values. * Cons: + Only works with arrays; not supported by all JavaScript engines for array-like objects. 3. Direct access (`A["X5"]`): * Pros: + Fast and efficient since it directly accesses the property without iteration. * Cons: + Requires the object to exist and have the property, which can lead to errors if not implemented correctly. **Library/Language Feature** There is no specific library used in this benchmark. However, some JavaScript engines (e.g., SpiderMonkey) may use optimized implementations for certain string checks. **Special JS Features** None mentioned explicitly. If you're interested in more advanced features like `forEach()`, `map()`, or `reduce()`, those are not specifically relevant to the performance comparison being tested here. **Other Alternatives** If you were looking for alternative approaches, some additional options could be: * Using a `Map` object with string keys and values. * Utilizing a `Set` object with string elements. * Implementing a custom loop using `for...of` or `Array.prototype.forEach()` to iterate over the array or array-like object. Keep in mind that these alternatives might not necessarily improve performance for this specific benchmark, as the focus is on comparing three basic approaches.
Related benchmarks:
Javascript unshift
Array.from vs Array spread with mapping of values2
lodash flatmap long
UINT32Array
indexof vs set123
Comments
Confirm delete:
Do you really want to delete benchmark?