Skip to content

Instantly share code, notes, and snippets.

@a-boertien
Forked from chathurawidanage/object_it_performance.js
Last active August 16, 2023 04:08
Show Gist options
  • Save a-boertien/0e370c528a36f1aafa52331f5870c15d to your computer and use it in GitHub Desktop.
Save a-boertien/0e370c528a36f1aafa52331f5870c15d 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');
const objectSize = 10000;
const iterations = 100;
console.log("Starting performance test with %d object size and %d iterations",
objectSize, iterations);
const values = {
ENTRIES: 0,
ENTRIES_FOR: 0,
KEYS: 0,
KEYS_FOR: 0,
KEYS_FOR_OF: 0,
VALUES: 0,
VALUES_FOR: 0,
FORIN: 0,
GETOWP: 0,
GETOWP_FOR: 0
}
const obs = new PerformanceObserver((items) => {
performance.clearMarks();
items.getEntries().forEach(entry => {
values[entry.name] += entry.duration;
});
console.table(values);
});
obs.observe({ entryTypes: ['measure'] });
function generateObject() {
const obj = {};
for (let i = 0; i < objectSize; i++) {
obj['key' + i] = 'val' + i;
}
return obj;
}
for (let i = 0; i < iterations; i++) {
const obj = generateObject();
//Object.entries
performance.mark('A');
Object.entries(obj).forEach(entry => {
const key = entry[0];
const value = entry[1];
});
performance.mark('B');
performance.measure('ENTRIES', 'A', 'B');
//Object.entries with for
performance.mark('A');
const entries = Object.entries(obj);
for (let i = 0; i < entries.length; i++) {
const key = entries[i][0];
const value = entries[i][1];
}
performance.mark('B');
performance.measure('ENTRIES_FOR', 'A', 'B');
//Object.Keys
performance.mark('A');
Object.keys(obj).forEach(key => {
const value = obj[key];
});
performance.mark('B');
performance.measure('KEYS', 'A', 'B');
//Object.Keys with for
performance.mark('A');
const keys = Object.keys(obj);
for (let i = 0; i < keys.length; i++) {
const value = obj[keys[i]];
}
performance.mark('B');
performance.measure('KEYS_FOR', 'A', 'B');
//Object.Keys with for..of
performance.mark('A');
for (const key of Object.keys(obj)) {
const value = obj[key];
}
performance.mark('B');
performance.measure('KEYS_FOR_OF', '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');
const values = Object.values(obj);
for (let i = 0; i < values.length; i++) {
const 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)) {
const value = obj[key];
}
}
performance.mark('B');
performance.measure('FORIN', 'A', 'B');
//Object.getOwnPropertyNames
performance.mark('A');
Object.getOwnPropertyNames(obj).forEach(key => {
const value = obj[key];
});
performance.mark('B');
performance.measure('GETOWP', 'A', 'B');
//Object.getOwnPropertyNames with for
performance.mark('A');
const props = Object.getOwnPropertyNames(obj);
for (let i = 0; i < props.length; i++) {
const value = obj[props[i]];
}
performance.mark('B');
performance.measure('GETOWP_FOR', 'A', 'B');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment