Skip to content

Instantly share code, notes, and snippets.

@ikhsanalatsary
Last active October 13, 2024 08:37
Show Gist options
  • Save ikhsanalatsary/831af20d68cd3f3a8d3e45c6f8ca9fb5 to your computer and use it in GitHub Desktop.
Save ikhsanalatsary/831af20d68cd3f3a8d3e45c6f8ca9fb5 to your computer and use it in GitHub Desktop.
lodash.merge vs object.assign vs object spread syntax in node js
let _ = require('lodash')
let { performance, PerformanceObserver } = require("perf_hooks")
let perfObserver = new PerformanceObserver((list, observer) => {
let durations = [];
let results = [];
list.getEntries().forEach((entry) => {
console.log(entry)
durations.push(entry.duration)
results.push({name: entry.name, duration: entry.duration})
})
let min = Math.min(...durations)
let max = Math.max(...durations)
var os = require('os');
console.log('test on machine ', process.platform, '-', process.arch)
console.log('OS type:', os.type());
console.log('OS version:', os.version());
console.log('test on node version ', process.version)
console.log('slowest ', results.find(e => e.duration === max).name, ' ', max)
console.log('fastest ', results.find(e => e.duration === min).name, ' ', min)
performance.clearMarks()
observer.disconnect()
})
perfObserver.observe({ entryTypes: ["measure"], buffered: true })
performance.mark("example-lodash-merge-start")
var a = { a: 'oh', b: 'my' };
var b = { c: 'goddess' };
var c = _.merge(a, b);
performance.mark("example-lodash-merge-end")
performance.measure("lodash", "example-lodash-merge-start", "example-lodash-merge-end")
performance.mark("example-spread-start")
var a = { a: 'oh', b: 'my' };
var b = { c: 'goddess' };
var c = { ...a, ...b };
performance.mark("example-spread-end")
performance.measure("spread", "example-spread-start", "example-spread-end")
performance.mark("example-object-assign-start")
var a = { a: 'oh', b: 'my' };
var b = { c: 'goddess' };
var c = Object.assign(a, b);
performance.mark("example-object-assign-end")
performance.measure("assign", "example-object-assign-start", "example-object-assign-end")
@ikhsanalatsary
Copy link
Author

based on sample codes:
measurethat.net

@Bug-Reaper
Copy link

Neat 🔥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment