Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Passing objects with raw values vs passing new objects with raw values vs passing just raw values vs array of values 2
(version: 0)
Updated to add 'Pass new object' & 'Pass re-created object', these reflect common real-world usage where the object contents are changing
Comparing performance of:
Pass object vs Pass raw values vs Pass array vs Pass new object vs Pass re-created object
Created:
3 years ago
by:
Guest
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);
Pass new object
x << 1; x ^= passObject({a, b, c});
Pass re-created object
object.a = a; object.b = b; object.c = c; x << 1; x ^= passObject(object);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Pass object
Pass raw values
Pass array
Pass new object
Pass re-created object
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 explores different ways to pass data between functions in JavaScript, focusing on performance. **Here's a breakdown:** * **Options Compared:** The benchmark tests five approaches: * **Pass Object:** Passing an existing object (`object`) containing the data. * **Pass Raw Values:** Passing individual variables (`a`, `b`, `c`) directly. * **Pass Array:** Passing an array containing the values. * **Pass New Object:** Creating a new object with the same values and passing that. * **Pass Re-created Object:** Modifying an existing object with the new values and then passing it. * **Purpose of Each Approach:** * **Passing Object:** Common practice when data is structured and related. Can be more concise but might introduce overhead if objects are large. * **Passing Raw Values:** Simple and efficient for small, unrelated pieces of data. * **Passing Array:** Suitable for passing multiple values together, particularly if they need to be iterated over later. * **Pros and Cons:** The benchmark aims to highlight the performance differences between these methods. * Passing raw values often has the best performance because it's the most direct data transfer. * Passing objects can have a slight performance penalty due to object creation and traversal. * Creating new objects or modifying existing ones adds extra overhead, making them potentially slower. **Other Considerations:** Readability and maintainability are also important factors when choosing how to pass data. Sometimes the most performant approach might not be the easiest to understand and work with. **Alternatives Not Explored in This Benchmark:** * **Functional Programming Approaches:** Techniques like using currying or passing functions as arguments can be performance-efficient but are not directly compared here. Let me know if you have any more questions!
Related benchmarks:
Variable assignment from object | traditional vs destructuring
Spread vs Object.assign (modify ) vs Object.assign (new)
Object.assign vs spread operatora
object.assign vs spread to create a copy
Spread operator vs Object.assign
Comments
Confirm delete:
Do you really want to delete benchmark?