Skip to content

Instantly share code, notes, and snippets.

@chathurawidanage
Last active August 16, 2023 03:47
Show Gist options
  • Save chathurawidanage/aa943e0cc654608e4c53d72115981596 to your computer and use it in GitHub Desktop.
Save chathurawidanage/aa943e0cc654608e4c53d72115981596 to your computer and use it in GitHub Desktop.
Script to compare performance of Object values iterating techniques | Performance Comparison [https://gists.cwidanage.com/2018/06/how-to-iterate-over-object-entries-in.html]
const { PerformanceObserver, performance } = require('perf_hooks');
let objectSize = 1000000;
let iterations = 100;
console.log("Starting performance test with %d object size and %d iterations",
objectSize, iterations);
let values = {
ENTRIES: 0,
ENTRIES_FOR: 0,
KEYS: 0,
KEYS_FOR: 0,
VALUES: 0,
VALUES_FOR: 0,
FORIN: 0,
GETOWP: 0,
GETOWP_FOR: 0
}
const obs = new PerformanceObserver((items) => {
let entry = items.getEntries()[0];
console.log(entry.name, entry.duration);
values[entry.name] += entry.duration;
performance.clearMarks();
});
obs.observe({ entryTypes: ['measure'] });
function generateObject() {
let obj = {};
for (let i = 0; i < objectSize; i++) {
obj['key' + i] = 'val' + i;
}
return obj;
}
for (let i = 0; i < iterations; i++) {
let obj = generateObject();
//Object.entries
performance.mark('A');
Object.entries(obj).forEach(entry => {
let key = entry[0];
let value = entry[1];
});
performance.mark('B');
performance.measure('ENTRIES', 'A', 'B');
//Object.entries with for
performance.mark('A');
let entries = Object.entries(obj);
for (let i = 0; i < entries.length; i++) {
let key = entries[i][0];
let value = entries[i][1];
}
performance.mark('B');
performance.measure('ENTRIES_FOR', 'A', 'B');
//Object.Keys
performance.mark('A');
Object.keys(obj).forEach(key => {
let value = obj[key];
});
performance.mark('B');
performance.measure('KEYS', 'A', 'B');
//Object.Keys with for
performance.mark('A');
let keys = Object.keys(obj);
for (let i = 0; i < keys.length; i++) {
let value = obj[keys[i]];
}
performance.mark('B');
performance.measure('KEYS_FOR', 'A', 'B');
//Object.Values
performance.mark('A');
Object.values(obj).forEach(value => {
});
performance.mark('B');
performance.measure('VALUES', 'A', 'B');
//Object.Values with for
performance.mark('A');
let values = Object.values(obj);
for (let i = 0; i < values.length; i++) {
let value = values[i];
}
performance.mark('B');
performance.measure('VALUES_FOR', 'A', 'B');
//For In
performance.mark('A');
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
let value = obj[key];
}
}
performance.mark('B');
performance.measure('FORIN', 'A', 'B');
//Object.getOwnPropertyNames
performance.mark('A', i);
Object.getOwnPropertyNames(obj).forEach(key => {
let value = obj[key];
});
performance.mark('B');
performance.measure('GETOWP', 'A', 'B');
//Object.getOwnPropertyNames with for
performance.mark('A');
let props = Object.getOwnPropertyNames(obj);
for (let i = 0; i < props.length; i++) {
let value = obj[props[i]];
}
performance.mark('B');
performance.measure('GETOWP_FOR', 'A', 'B');
}
console.log(Object.entries(values).sort((a, b) => {
return a[1] - b[1];
}).map(obj => {
obj[1] /= iterations;
return obj;
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment