Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Passing objects with raw values vs passing just raw values vs array of values 2
(version: 0)
Updated because it looks like Firefox optimizes away the entire test
Comparing performance of:
Pass object vs Pass raw values vs Pass array
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var a = "Here's a string value"; var b = 5; // and a number var c = false; var object = { a, b, c } var array = [ a, b, c ]; var passObject = (obj) => { return obj.a.length + obj.b * obj.c ? 2 : 1; } var passRawValues = (val_a, val_b, val_c) => { return val_a.length + val_b * val_c ? 2 : 1; } var passArray = (arr) => { return arr[0].length + arr[1] * arr[2] ? 2 : 1; } var x = 0;
Tests:
Pass object
x << 1; x ^= passObject(object);
Pass raw values
x << 1; x ^= passRawValues(a, b, c);
Pass array
x << 1; x ^= passArray(array);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Pass object
Pass raw values
Pass array
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
Browser/OS:
Chrome 141 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Pass object
5837354.5 Ops/sec
Pass raw values
5864915.0 Ops/sec
Pass array
5899957.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and benchmark test cases to understand what is being tested. **Benchmark Definition** The benchmark measures the performance of three different approaches to passing objects or arrays in JavaScript: 1. **Passing objects**: The `passObject` function takes an object as input, accesses its properties (`a`, `b`, and `c`) using dot notation, and performs a conditional expression on their values. 2. **Passing raw values**: The `passRawValues` function takes three separate arguments (`val_a`, `val_b`, and `val_c`) and uses the same conditional expression as in the `passObject` function. 3. **Passing arrays**: The `passArray` function takes an array as input, accesses its elements using indexing (0-based), and performs the same conditional expression. **Options compared** The benchmark compares the performance of these three approaches: * `passObject`: Passing an object with properties * `passRawValues`: Passing raw values as separate arguments * `passArray`: Passing an array as a single entity **Pros and Cons of each approach:** 1. **Passing objects (`passObject`)**: * Pros: Object property access is more intuitive and readable. * Cons: May lead to slower performance due to the overhead of accessing object properties using dot notation. 2. **Passing raw values (`passRawValues`)**: * Pros: Raw value access can be faster since it avoids the overhead of accessing object properties. * Cons: Requires explicit passing of each argument, which may lead to errors or typos. 3. **Passing arrays (`passArray`)**: * Pros: Array element access is often more efficient than property access. * Cons: May not be as intuitive for developers who are not familiar with array indexing. **Library and special JavaScript features** The benchmark uses the following library: 1. None explicitly mentioned, but it seems that the script preparation code includes a custom function `passObject`, `passRawValues`, and `passArray` which should work in most modern browsers without additional libraries. No special JavaScript features or syntax are used beyond what's standard in modern JavaScript (e.g., arrow functions, template literals). **Other alternatives** Some alternative approaches could be considered: * Passing objects as arrays (`Object.values()` or `Object.entries()`) * Using destructuring assignment to extract properties or elements from an object or array * Utilizing methods like `JSON.stringify()` for faster property access However, these alternatives may not provide significant performance benefits and might introduce additional complexity. Keep in mind that the benchmark's results are specific to this particular test case, so it's essential to consider other factors when optimizing JavaScript code, such as function call overhead, memory allocation, and caching.
Related benchmarks:
Spread Vs Unshift into new array
empty an array in JavaScript - splice vs setting length
Array.from vs Array spread with mapping of values
empty an array in JavaScript - splice vs setting length. 444
Array.push(x) vs array[n]=x
Comments
Confirm delete:
Do you really want to delete benchmark?