Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test return object vs return array vs return seal object
(version: 0)
Test returning a plain object vs returning an array of items vs returning a sealed object vs returning a cached sealed object
Comparing performance of:
returnObject vs returnFrozenObject vs returnSealedObject vs returnCachedSealedObject vs returnArray
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function returnObject() { return { a: parseInt(Math.random() * 32, 10), b: parseInt(Math.random() * 132, 10), c: parseInt(Math.random() * 232, 10), d: parseInt(Math.random() * 332, 10), e: parseInt(Math.random() * 432, 10), } } function returnSealedObject() { return Object.seal({ a: parseInt(Math.random() * 32, 10), b: parseInt(Math.random() * 132, 10), c: parseInt(Math.random() * 232, 10), d: parseInt(Math.random() * 332, 10), e: parseInt(Math.random() * 432, 10), }) } function returnFrozenObject() { return Object.freeze({ a: parseInt(Math.random() * 32, 10), b: parseInt(Math.random() * 132, 10), c: parseInt(Math.random() * 232, 10), d: parseInt(Math.random() * 332, 10), e: parseInt(Math.random() * 432, 10), }) } const objCache = Object.seal({ a: 0, b: 0, c: 0, d: 0, e: 0, }) function returnCachedSealedObject() { objCache.a = parseInt(Math.random() * 32, 10) objCache.b = parseInt(Math.random() * 132, 10) objCache.c = parseInt(Math.random() * 232, 10) objCache.d = parseInt(Math.random() * 332, 10) objCache.e = parseInt(Math.random() * 432, 10) return objCache } function returnArray() { return [ parseInt(Math.random() * 32, 10), parseInt(Math.random() * 132, 10), parseInt(Math.random() * 232, 10), parseInt(Math.random() * 332, 10), parseInt(Math.random() * 432, 10), ] }
Tests:
returnObject
returnObject()
returnFrozenObject
returnFrozenObject()
returnSealedObject
returnSealedObject()
returnCachedSealedObject
returnCachedSealedObject()
returnArray
returnArray()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
returnObject
returnFrozenObject
returnSealedObject
returnCachedSealedObject
returnArray
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.2:3b
, generated one year ago):
Let's break down what's being tested in the provided benchmark. **Benchmark Definition** The benchmark is testing different approaches to returning values from JavaScript functions: 1. **Plain object**: `returnObject()` 2. **Array of items**: `returnArray()` 3. **Sealed object**: `returnSealedObject()` 4. **Cached sealed object**: `returnCachedSealedObject()` (note: this is a variant of the sealed object approach, where the object is cached and modified before returning it) **Options compared** The benchmark compares the execution performance of these four approaches: * Plain object (`returnObject()`) * Array of items (`returnArray()`) * Sealed object (`returnSealedObject()`) * Cached sealed object (`returnCachedSealedObject()`) **Pros and Cons** Here are some pros and cons for each approach: 1. **Plain object (`returnObject()`)** * Pros: simple, straightforward implementation * Cons: may require more memory allocation and garbage collection, potentially leading to slower execution 2. **Array of items (`returnArray()`)** * Pros: easy to implement, can be faster due to array caching and optimized memory management * Cons: may require additional memory allocation for the array, and may lead to slower execution if the array is not properly cached 3. **Sealed object (`returnSealedObject()`)** * Pros: provides some level of protection against accidental modifications, can be faster than plain objects due to optimized memory management * Cons: adds overhead due to the `Object.seal()` method, which may lead to slower execution 4. **Cached sealed object (`returnCachedSealedObject()**)** * Pros: combines the benefits of sealed objects and caching, potentially leading to faster execution * Cons: requires additional memory allocation for the cached object, and may add overhead due to the `objCache` initialization **Library usage** The benchmark uses the `Object.seal()` method, which is a built-in JavaScript function that seals an object, making it immutable and preventing accidental modifications. **Special JS feature or syntax** There is no special JS feature or syntax used in this benchmark. The focus is on comparing different approaches to returning values from functions. **Alternatives** Other alternatives for returning values from functions could include: * Using a custom data structure, such as a struct-like object * Returning a primitive value (e.g., number, string) * Using a streaming approach, where the function returns an iterator or generator instead of a fixed-size array or object The benchmark provides a simple and straightforward way to compare the performance of different approaches to returning values from functions in JavaScript.
Related benchmarks:
JavaScript spread operator vs Object.assign performance for Empty Object
JavaScript spread operator vs Object.assign performance (single addition)
JavaScript spread operator vs Object.assign performance - Kien Nguyen
Object.assign() vs spread operator (New object)
Spread Operator VS Object.assign performance analysis
Comments
Confirm delete:
Do you really want to delete benchmark?