Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object for...in vs for...index with prototype
(version: 1)
Comparing performance of:
forIn vs forIndex
Created:
6 months ago
by:
Guest
Jump to the latest result
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(1e6);
Tests:
forIn
testForIn(obj)
forIndex
testObjectKeysFor(obj)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
forIn
forIndex
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
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/OS:
Chrome 142 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
forIn
5.1 Ops/sec
forIndex
7.7 Ops/sec
Related benchmarks:
Checking property existence
destructuring vs for-in
Object Spread vs New property
for-in vs object.keys (own props)
Map vs Object creation 2
Map vs Object creation 3
destructuring vs creating
Prototype chains
Object for...in vs for...index with prototype 2
Comments
Confirm delete:
Do you really want to delete benchmark?