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
llama3.1:latest
, generated one year ago):
Let's dive into the benchmark! **Benchmark Definition** The benchmark is called "array vs function" and it compares two approaches to accessing a value in an object. **Script Preparation Code** The code defines an object `Side` with two properties: `FROM` and `TO`. It also defines a function `getOtherSide(side)` that returns the opposite side based on the input. Finally, it creates an object `otherSide` that maps each side to its opposite. **Individual Test Cases** There are two test cases: 1. **"function"**: This test case uses the `getOtherSide(Side.FROM)` expression to access the value of `TO`. In other words, it calls a function to get the opposite side. 2. **"array"**: This test case uses the `otherSide[Side.FROM]` expression to access the value of `TO` directly from an array. **Latest Benchmark Result** The benchmark results show that: * The "array" approach executes at 3085803.5 times per second on a Yandex Browser 20 desktop browser. * The "function" approach executes at 1886867.125 times per second on the same browser. **What's being tested?** In this benchmark, we're comparing two approaches to accessing a value in an object: 1. **Function call**: Calling a function `getOtherSide(side)` to get the opposite side. 2. **Array lookup**: Directly looking up the value of `TO` from an array `otherSide`. **What are the pros and cons?** * **Function call**: + Pros: The code is more readable, as it's clear what the function does. + Cons: Function calls can be slower than direct lookups, as they involve additional overhead. * **Array lookup**: + Pros: Direct lookups are generally faster than function calls. + Cons: The code might be less readable, as it requires knowledge of the underlying array structure. **Other considerations** When to use each approach: * Use a function call when: - You need to perform some logic or validation before accessing the value. - You want to make the code more readable and maintainable. * Use an array lookup when: - You're working with large amounts of data and performance is critical. - The underlying structure is well-known and easy to understand. **What library is being used?** None in this benchmark. The code only uses built-in JavaScript features. **Special JS feature or syntax?** No special features or syntax are used in this benchmark. **Other alternatives?** You could also consider using a **computed property name**, like `otherSide[Side.FROM] = Side.TO`, to achieve the same result as the array lookup. This approach has its own pros and cons, but it's an alternative way to access values from an object.
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?