<script src='https://cdnjs.cloudflare.com/ajax/libs/immutable/4.0.0-rc.12/immutable.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
const obj1 = {mamka: 'big', jopa: 'fat'}
const obj2 = {obj1}
const obj1 = {mamka: 'big', jopa: 'fat'}
const obj2 = Object.assign({}, obj1)
const obj1 = {mamka: 'big', jopa: 'fat'}
const obj2 = _.extend({}, obj1)
const rec1 = Immutable.Record({mamka: 'big', jopa: 'fat'});
const rec2 = rec1({mamka: 'small', jopa: 'thin'})
var obj1 = {
mamka: 'big',
jopa: 'fat'
};
var obj2 = _objectSpread({}, obj1);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object spread | |
Object.assign | |
_.extend | |
immutable record | |
Babelified spread |
Test name | Executions per second |
---|---|
Object spread | 65100160.0 Ops/sec |
Object.assign | 5850857.0 Ops/sec |
_.extend | 2643322.0 Ops/sec |
immutable record | 161685.9 Ops/sec |
Babelified spread | 979098.4 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Definition
The benchmark definition is a JSON object that describes the test case being run. In this case, we have four individual test cases:
Each test case has a brief description and two code snippets: a script preparation code and an HTML preparation code.
Script Preparation Code
The script preparation code is a JavaScript function that sets up the environment for each test case. In this case, we have:
_objectSpread
function (a custom implementation of object spread)_defineProperty
function (another custom implementation)These functions are used to create objects and manipulate their properties.
Html Preparation Code
The HTML preparation code includes two external scripts:
Test Cases
Let's analyze each test case:
obj1
with two properties: mamka
and jopa
. Then, it creates a new object obj2
by spreading the properties of obj1
using the _objectSpread
function.Pros: Simple and straightforward implementation. Cons: May not be as efficient as other methods due to the custom implementation.
obj1
with two properties: mamka
and jopa
. Then, it creates a new object obj2
by assigning the properties of obj1
using the Object.assign()
method.Pros: Widely supported and efficient. Cons: May not work as expected if the source object has circular references or other issues.
_extend
function from Lodash to create a new object by extending an initial object obj1
.Pros: Easy to use and works well with many data structures. Cons: May have performance overhead due to Lodash's complexity.
obj1
using the _objectSpread
function from Babel.Pros: Fast and efficient, thanks to Babel's optimization. Cons: May not work as expected in older browsers or environments that don't support modern JavaScript features.
mamka
and jopa
.Pros: Efficient and predictable, thanks to Immutable.js's immutability. Cons: May have higher overhead due to the need to clone objects.
Other Considerations
When choosing between these methods, consider factors like:
Alternatives
If you're looking for alternatives to these methods, consider:
Object.create()
method instead of Object.assign()Keep in mind that each approach has its strengths and weaknesses. MeasureThat.net's microbenchmarking can help you determine which method is best suited for your specific use case.