Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Javascript Object vs Array.indexOf
(version: 0)
Comparing performance of:
indexOf vs Object
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var object = { A : "A", B : "B", C : "C" }; var array = [ "A", "B", "C" ];
Tests:
indexOf
array.indexOf("C");
Object
object["C"]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
indexOf
Object
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/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
indexOf
84928560.0 Ops/sec
Object
142337488.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its components. **What is tested?** The provided JSON represents a JavaScript microbenchmark that compares two approaches: 1. **Object property access**: `object[\"C\"]` 2. **Array index of**: `array.indexOf(\"C\")` Both tests aim to measure how fast each approach can execute, but they serve different purposes. **Options compared:** * Object property access (`object[\"C\"]`): + Pros: - Directly accesses the desired property in an object. - Might be faster for large objects or when accessing a specific property is common. + Cons: - Can lead to slower performance if the object is large and not optimized for property access (e.g., due to overhead of JavaScript engine's lookup mechanisms). - May not work as expected with certain types of objects (e.g., arrays, functions) or in specific browsers/engines. * Array index of (`array.indexOf(\"C\")`): + Pros: - Efficiently searches for a value in an array. - Works well for large arrays and can handle edge cases like duplicate values. + Cons: - Can be slower than object property access when searching for the first occurrence of a value (as it has to iterate through the entire array). - May not work as expected with certain types of arrays (e.g., sparse arrays) or in specific browsers/engines. **Library and purpose:** There is no explicit library mentioned in the benchmark definition. However, `array.indexOf()` is a built-in JavaScript method that uses the `Map` data structure internally to perform fast searches. **Special JS feature or syntax:** There are no special JavaScript features or syntax mentioned in the benchmark definition. The tests only use basic JavaScript syntax and do not include any experimental or deprecated features. **Other alternatives:** If you want to compare different approaches, here are a few more options: 1. **Using `in` operator**: `object['C']` 2. **Using bracket notation with optional chaining**: `object?.C` 3. **Using a for...of loop**: `for (const value of object) { if (value === 'C') break; }` Each of these alternatives has its own trade-offs and might be faster or slower than the original benchmark in different scenarios. Keep in mind that microbenchmarks can have various factors affecting their results, such as: * Hardware and operating system specifics * Browser engine optimizations * JavaScript version and engine versions * Object creation and size When interpreting benchmark results, it's essential to consider these factors and not just the raw execution time numbers.
Related benchmarks:
IndexOf vs Includes
IndexOf vs Includes array of numbers
Array constructor vs literal performance, 12345
JS indexOf vs some
Comments
Confirm delete:
Do you really want to delete benchmark?