Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.indexOf vs String.indexOf vs Object lookup
(version: 0)
Comparing performance of:
Array indexOf vs String indexOf vs Object lookup
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var o = {'ff1234567890121':0, 'ff1234567890122':1, 'ff1234567890123':2, 'ff1234567890124':3, 'ff1234567890125':4}; var a = ['ff1234567890121', 'ff1234567890122', 'ff1234567890123', 'ff1234567890124', 'ff1234567890125']; var s = '-ff1234567890121--ff1234567890122--ff1234567890123--ff1234567890124--ff1234567890125-'; var t = 'ff1234567890124';
Tests:
Array indexOf
var i = a.indexOf(t);
String indexOf
var i = s.indexOf('-' + t + '-');
Object lookup
var i = o[t];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array indexOf
String indexOf
Object lookup
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array indexOf
78815256.0 Ops/sec
String indexOf
156970352.0 Ops/sec
Object lookup
149132320.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark. **Benchmark Goal** The goal of this benchmark is to compare the performance of three different methods for finding the index of a value within an array or string: 1. `Array.indexOf()` 2. `String.indexOf()` 3. `Object.lookup` (which is actually just using bracket notation, e.g., `o[t]`, but we'll discuss this later) **Options Compared** * `Array.indexOf()`: + Pros: Widely supported, easy to use, and optimized for performance. + Cons: May have a higher overhead due to its existence in the browser's core library. * `String.indexOf()`: + Pros: Optimized for performance and often faster than `Array.indexOf()` on strings. + Cons: Limited support on arrays; may not work correctly with certain Unicode characters. * `Object.lookup` (bracket notation): + Pros: Fast, flexible, and widely supported. Can be used on any object, including custom ones. + Cons: May require more setup and manual consideration of edge cases. **Library/Functionality** * `Array.indexOf()`: A built-in function in JavaScript's Array prototype. * `String.indexOf()`: A built-in function in JavaScript's String prototype. * `Object.lookup` (bracket notation): Not a traditional library, but rather an optimization technique that leverages the way objects store and retrieve property values. **Special JS Feature/Syntax** None mentioned explicitly. However, `Object.lookup` relies on the fact that objects in JavaScript can be accessed using bracket notation (`o[t]`), which is a shorthand for `o.hasOwnProperty(t) ? o[t] : undefined`. This syntax is widely supported but not unique to modern JavaScript versions. **Benchmark Considerations** The benchmark measures execution time per second for each of the three methods. The test data consists of an array and string containing a single value that's then searched for within those collections. **Alternative Approaches** Other optimization techniques or approaches could be explored, such as: * Using `Array.prototype.reduce()` to find the index * Implementing custom binary search algorithms for arrays * Utilizing hardware-accelerated string comparison on certain platforms However, these alternatives are not directly related to the specific methods being compared in this benchmark. **Additional Insights** Keep in mind that `Object.lookup` (bracket notation) is actually just a clever way of writing `o[t]`. It's a performance optimization technique that takes advantage of how objects store property values. This approach can be faster than using the traditional `hasOwnProperty()` method or accessing properties directly. In conclusion, this benchmark provides a useful comparison of three common methods for finding indices within arrays and strings, highlighting their relative performance characteristics.
Related benchmarks:
Array.indexOf vs String.indexOf
Array.indexOf vs Object lookup small collection
Array IndexOf vs includes
Array includes vs string indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?