Skip to content

Instantly share code, notes, and snippets.

@robinheghan
Last active October 14, 2016 22:32
Show Gist options
  • Save robinheghan/51b42ac2c4df1776d7a77e401b7477c4 to your computer and use it in GitHub Desktop.
Save robinheghan/51b42ac2c4df1776d7a77e401b7477c4 to your computer and use it in GitHub Desktop.
Benchmark of Elm update function
// 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