Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array vs function
(version: 0)
Comparing performance of:
function vs array
Created:
6 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
Side = {FROM:'FROM',TO:'TO'} function getOtherSide(side) { return side === Side.FROM ? Side.TO : Side.FROM; } otherSide = {[Side.FROM]:Side.TO,[Side.TO]:Side.FROM}
Tests:
function
var y = getOtherSide(Side.FROM);
array
var x = otherSide[Side.FROM];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
function
array
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
gemma2:9b
, generated one year ago):
This benchmark tests the performance difference between accessing values using a JavaScript function and accessing them through an array. **Options being compared:** * **Function Approach:** - A simple function `getOtherSide(side)` is defined to determine the opposite value based on an input (`FROM` or `TO`). When you call this function, it performs a conditional check (ternary operator) and returns the desired opposite side. * **Array Approach:** - An object literal is created in the form of an array: `otherSide = {[Side.FROM]:Side.TO,[Side.TO]:Side.FROM}`. This effectively creates a mapping where keys are "FROM" and "TO", and values are their opposites. Accessing the opposite side then involves using bracket notation to look up the value based on the key. **Pros and Cons:** * **Function Approach:** - **Pro:** Can be more readable for simple logic. - **Con:** Involves function call overhead, which can be slightly slower than direct array access. * **Array Approach:** - **Pro:** Typically faster due to direct property lookup (no function call). - **Con:** Can lead to less clear code if the logic is complex and involves multiple checks or steps. **Other Considerations:** - The specific performance difference between these approaches can vary depending on several factors, including the JavaScript engine being used, optimization levels, and other code running alongside the benchmark. Let me know if you have any more questions about this benchmark!
Related benchmarks:
Array.prototype.slice Versus Spread Operator
Array clone from index 1 to end: spread operator vs slice
Array.prototype.slice vs Array.from()
Array.prototype.slice vs spread operator for copying
Array.prototype.slice vs spread operator performance
Comments
Confirm delete:
Do you really want to delete benchmark?