Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Object for...in vs for...index with prototype 2
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 Edg/142.0.0.0
Browser:
Chrome 142
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
6 months ago
Test name
Executions per second
forIn
27824.0 Ops/sec
forIndex
61854.0 Ops/sec
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
// 生成测试对象 const createTestObject = (size) => { // 父对象(原型) const parent = { parentProp1: 'parent', parentProp2: 'parent2', parentProp3: {}, parentProp4: [], parentMethod1() { return 'parentMethod'; }, parentMethod2() { return 'parentMethod'; }, parentMethod3() { return 'parentMethod'; }, }; // 子对象(继承 Parent) const child = Object.create(parent); for (let i = 0; i < size; i++) { child[`key${i}`] = i; } return child; }; // 测试 for...in function testForIn(obj) { const start = performance.now(); for (const key in obj) { if (Object.hasOwn(obj, key)) { const value = obj[key]; } } return performance.now() - start; } // 测试 Object.keys + for function testObjectKeysFor(obj) { const start = performance.now(); const keys = Object.keys(obj); for (let i = 0; i < keys.length; i++) { const key = keys[i]; const value = obj[key]; } return performance.now() - start; } // 运行测试 const obj = createTestObject(1e3);
Tests:
forIn
testForIn(obj)
forIndex
testObjectKeysFor(obj)