Created
April 22, 2019 17:54
-
-
Save bmeurer/0dae4a6b0e23f43d5a22d7c91476b6c0 to your computer and use it in GitHub Desktop.
Micro-benchmark to measure the performance different of Object.assign() vs. object spread
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2013-2019 Benedikt Meurer | |
// | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// <https://www.apache.org/licenses/LICENSE-2.0> | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. | |
if (typeof console === 'undefined') console = {log:print}; | |
var INPUT = { | |
"_id": "5c49a3f586a314e1da9bc5c4", | |
"index": 0, | |
"guid": "4a19223f-e806-4590-b0b2-bc52854010e9", | |
"isActive": true, | |
"balance": "$1,341.65", | |
"picture": "http://placehold.it/32x32", | |
"age": 33, | |
"eyeColor": "brown", | |
"name": "English Peck", | |
"gender": "male", | |
"company": "APPLIDECK", | |
"email": "englishpeck@applideck.com", | |
"phone": "+1 (995) 574-3187", | |
"address": "479 Lloyd Court, Cecilia, District Of Columbia, 4378", | |
"about": "Sunt laborum commodo ex nisi. Sint amet sit excepteur qui do veniam qui sint sit labore id. Laboris laborum amet eiusmod laborum duis.\r\n", | |
"registered": "2017-02-11T03:09:56 -01:00", | |
"latitude": 31.939115, | |
"longitude": -54.305863, | |
"tags": [ | |
"laborum", | |
"laboris", | |
"cupidatat", | |
"nostrud", | |
"dolore", | |
"ex", | |
"duis" | |
], | |
"friends": [ | |
{ | |
"id": 0, | |
"name": "Susie Sawyer" | |
}, | |
{ | |
"id": 1, | |
"name": "Lola Ware" | |
}, | |
{ | |
"id": 2, | |
"name": "Bender Barrera" | |
} | |
], | |
"greeting": "Hello, English Peck! You have 10 unread messages.", | |
"favoriteFruit": "banana" | |
}; | |
const TESTS = []; | |
const N = 1e6; | |
(function() { | |
TESTS.push( | |
function objectSpreadFirst(input) { | |
return {...input, x: 1}; | |
}, | |
function objectAssignFirst(input) { | |
return Object.assign({}, input, {x:1}); | |
}, | |
function objectSpreadLast(input) { | |
return {x: 1, ...input}; | |
}, | |
function objectAssignLast(input) { | |
return Object.assign({}, {x:1}, input); | |
} | |
); | |
})(); | |
function test(fn) { | |
var result; | |
for (var i = 0; i < N; ++i) { | |
result = fn(INPUT); | |
} | |
return result; | |
} | |
test(x => x); // Pollute call feedback early on. | |
for (var j = 0; j < TESTS.length; ++j) { | |
test(TESTS[j]); | |
} | |
for (var j = 0; j < TESTS.length; ++j) { | |
var startTime = Date.now(); | |
test(TESTS[j]); | |
console.log(`${TESTS[j].name}: ${Date.now() - startTime} ms.`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment