Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
object create
(version: 0)
Comparing performance of:
object.assign vs keys vs for in
Created:
8 years ago
by:
Guest
Jump to the latest result
Tests:
object.assign
var obj1 = {foo: 'bar', that: 1, type: true}; var obj2 = Object.assign({}, obj1, {type: false});
keys
var obj1 = {foo: 'bar', that: 1, type: true}; var obj2 = Object.keys(obj1).map(key => obj1[key]); obj2.type = false;
for in
var obj1 = {foo: 'bar', that: 1, type: true}; var obj2 = {}; for(var key in obj1) { if(!obj1.hasOwnProperty(key)) { return; } obj2[key] = obj1[key]; } obj2.type = false;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
object.assign
keys
for in
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 the provided benchmark and its results. **Benchmark Overview** The benchmark tests three different approaches to create an object in JavaScript: 1. Using `Object.assign()` 2. Using `Array.prototype.keys()` (also known as "keys" in the benchmark) 3. Using a traditional `for...in` loop These approaches are compared to measure their performance differences. **Options Compared** The options being compared are: * **Object.assign()**: A built-in method that creates a new object by copying properties from an existing object. * **Array.prototype.keys()**: A method that returns an array of property names of the given object. The "keys" approach uses this method to create a new object by mapping over the property names. * **Traditional `for...in` loop**: This is a basic approach that iterates over the properties of an object using a traditional loop. **Pros and Cons** Here are some pros and cons for each approach: * **Object.assign()**: + Pros: Concise, efficient, and widely supported. + Cons: May not work as expected if the source object has a non-standard property name or if it's not an object (e.g., a string). * **Array.prototype.keys()**: + Pros: Modern, concise, and easy to read. It avoids issues with non-standard property names. + Cons: May be slower than `Object.assign()` due to the overhead of creating an array of property names. * **Traditional `for...in` loop**: + Pros: Well-established, easy to understand, and works in older browsers. + Cons: More verbose, may have performance issues due to the loop. **Library Usage** There is no explicit library usage mentioned in the benchmark definition or test cases. However, the use of `Array.prototype.keys()` implies a modern JavaScript feature that was introduced with ECMAScript 2015 (ES6). **Special JS Feature/Syntax** The "keys" approach uses the `Array.prototype.keys()` method, which is a modern JavaScript feature introduced in ES6. This method returns an array-like object containing the property names of the given object. **Alternatives** Other alternatives for creating objects in JavaScript include: * Using object literal notation (e.g., `{ foo: 'bar', that: 1 }`) * Using the `Object.create()` method * Using a constructor function to create objects These alternatives may have different performance characteristics or trade-offs, but they are not explicitly compared in this benchmark.
Related benchmarks:
Thingie
object create vs others
javascript new vs Object.create
javascript new vs Object.create 2
javascript new vs Object.create 3
Comments
Confirm delete:
Do you really want to delete benchmark?