Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS if + array binary vs object + if
(version: 0)
JS indexOf + array vs object + if
Comparing performance of:
indexOf + array vs object + if
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; var obj = {};
Tests:
indexOf + array
arr[0] = 1; arr[1] = 0; arr[2] = 0; arr[3] = 1; if(arr[0]){ console.log('a'); } if(arr[1]){ console.log('b'); } if(arr[2]){ console.log('c'); } if(arr[3]){ console.log('d'); }
object + if
obj.a = true; obj.e = true; obj.f = true; obj.d = true; if(obj.a){ console.log('a'); } if(obj.b){ console.log('b'); } if(obj.c){ console.log('c'); } if(obj.d){ console.log('d'); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
indexOf + array
object + if
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; SAMSUNG SM-J810G) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/16.0 Chrome/92.0.4515.166 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 92 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
indexOf + array
6285.5 Ops/sec
object + if
5969.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **What is being tested?** The provided benchmark compares two approaches: using `indexOf` on an array and using `if-conditional checks` with objects. For the array approach, the test case iterates over an array of length 4, setting each element to a specific value. It then checks if each element is truthy (i.e., not equal to 0) using `if` statements. The goal is to find the fastest way to execute these checks. For the object approach, a similar test case uses objects instead of arrays. Each property (a, e, f, and d) is set to `true`. The test then checks if each property name is truthy by accessing its value using dot notation (`obj.a`, etc.) with `if` statements. **Options compared** The two approaches are: 1. **Array indexing**: Using the `indexOf()` method on an array to check for existence. 2. **Object-based conditional checks**: Directly checking object property names using `if` statements. **Pros and Cons of each approach:** **Array indexing** Pros: * Fast, as it uses a built-in function optimized for performance * Efficient use of memory Cons: * May not be applicable in all situations (e.g., when the array has more than two elements) * Requires a separate call to `indexOf()` for each check **Object-based conditional checks** Pros: * Can be applied to any number of properties * More intuitive when working with objects, as it directly accesses property names Cons: * May not be optimized for performance compared to `indexOf()` * Requires more memory to store the object **Library and special JS feature: None mentioned in this benchmark.** Now, let's look at the latest benchmark results. The results show that Chrome Mobile 92 executed the array-based approach slightly faster (6285.51953125 executions per second) compared to the object-based approach (5969.4189453125 executions per second). When considering these results, keep in mind that: * The specific hardware and software configuration of the test device (Android 10, Samsung SM-J810G, Chrome Mobile 92) might influence performance. * The size of the array/object used in this benchmark is relatively small. **Alternatives:** In theory, other approaches could be compared, such as: 1. **Using `includes()` instead of `indexOf()`:** This method checks if a value exists within an array, and its performance characteristics might differ from `indexOf()`. 2. **Other conditional check methods:** There are various ways to implement conditional checks in JavaScript, such as using `in` operator or more complex logic with bitwise operators. 3. **Using different data structures:** Other data structures like Sets or Maps could potentially change the results of this benchmark. However, considering the simplicity and directness of the current test cases, it's likely that the results will remain consistent across different implementations and optimizations.
Related benchmarks:
Array isArray vs instanceof
Array isArray vs typeof
JS array emptiness check
instanceof Array vs Array.isArray
Array isArray vs Object.prototype
Comments
Confirm delete:
Do you really want to delete benchmark?