Last active
October 14, 2016 22:32
-
-
Save robinheghan/51b42ac2c4df1776d7a77e401b7477c4 to your computer and use it in GitHub Desktop.
Benchmark of Elm update function
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
// npm install benchmark | |
var bench = require('benchmark'); | |
function updateOriginal(oldRecord, updatedFields) | |
{ | |
var newRecord = {}; | |
for (var key in oldRecord) | |
{ | |
newRecord[key] = (key in updatedFields) ? updatedFields[key] : oldRecord[key]; | |
} | |
return newRecord; | |
} | |
function updateNew(oldRecord, updatedFields) | |
{ | |
var newRecord = {}; | |
for (var key in oldRecord) | |
{ | |
newRecord[key] = oldRecord[key]; | |
} | |
for (var key in updatedFields) { | |
newRecord[key] = updatedFields[key]; | |
} | |
return newRecord; | |
} | |
function updateTypeof(oldRecord, updatedFields) | |
{ | |
var newRecord = {}; | |
for (var key in oldRecord) | |
{ | |
var val = updatedFields[key]; | |
newRecord[key] = (typeof val !== 'undefined') ? val : oldRecord[key]; | |
} | |
return newRecord; | |
} | |
var suite = new bench.Suite; | |
var baseObj = { | |
name: "Robin", | |
age: 27, | |
hobbies: ["football", "chess", "programming"], | |
male: true, | |
occupation: "student", | |
phoneNumber: 12345678, | |
email: "[email protected]" | |
}; | |
var updateObj = { | |
age: 28 | |
}; | |
suite.add('original', function () { | |
updateOriginal(baseObj, updateObj) | |
}).add('new', function () { | |
updateNew(baseObj, updateObj); | |
}).add('typeof', function () { | |
updateTypeof(baseObj, updateObj); | |
}).on('cycle', function (event) { | |
console.log(String(event.target)); | |
}).run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment