Object.entries VS Object.keys VS Object.values
Date tested:
2 years ago
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0
Test name
Executions per second
Object.entries
49984.9 Ops/sec
Object.keys
33700.0 Ops/sec
Object.values
29811.2 Ops/sec
Object.entries2
51384.6 Ops/sec
Benchmark definition (click to collapse):
Script Preparation code:
function makeid() { var text = ""; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; for (var i = 0; i < 5; i++) text += possible.charAt(Math.floor(Math.random() * possible.length)); return text; } window.parentObj = {}; for (let i = 0; i < 100; i++) { window.parentObj[makeid()] = makeid(); }
Tests:
Object.entries
const newObj = {}; Object.entries(window.parentObj).forEach(([k, v], i) => { if ((i % 2) === 0) { newObj[k] = v; } });
Object.keys
const newObj = {}; Object.keys(window.parentObj).forEach((k, i) => { if ((i % 2) === 0) { newObj[k] = window.parentObj[k]; } });
Object.values
const newObj = {}; Object.values(window.parentObj).forEach((v, i) => { if ((i % 2) === 0) { newObj[i] = window.parentObj[v]; } });
Object.entries2
const newObj = {}; Object.entries(window.parentObj).forEach(([k, v], i) => { if ((i % 2) === 0) { newObj[i] = v; } });
Open this result on MeasureThat.net