Last active
October 14, 2016 22:56
-
-
Save endam/8e8f7e167ac3a59ce732fd376f356294 to your computer and use it in GitHub Desktop.
JavaScriptで配列やオブジェクトをループする良い方法を真剣に検討してみた ref: http://qiita.com/endam/items/808a084859e3a101ab8f
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let ar = [10, 20, 30]; | |
for(let i = 0; i < ar.length; i++) { | |
console.log(ar[i]) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for(let i in ar) { | |
console.log(ar[i]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let obj = {test1:10, test2:20, test3:30}; | |
// prototype拡張 | |
Object.prototype.extendFunc = function(){}; | |
console.log("for...in") | |
for(let k in obj) { | |
console.log(obj[k]); | |
} | |
console.log("-----------------"); | |
console.log("for...of"); | |
for(let k of Object.keys(obj)) { | |
console.log(obj[k]); | |
} | |
console.log("-----------------"); | |
console.log("Array.forEach"); | |
Object.keys(obj).forEach(function (k) { | |
console.log(obj[k]); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for...in | |
10 | |
20 | |
30 | |
[Function] | |
----------------- | |
for...of | |
10 | |
20 | |
30 | |
----------------- | |
Array.forEach | |
10 | |
20 | |
30 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for(let k in obj) { | |
if(obj.hasOwnProperty(k)) { | |
console.log(k+ ':' + obj[k]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// テスト用配列作成 | |
let ar = []; | |
for(let i = 0; i < 100; i++) { | |
ar[i] = "test"; | |
} | |
console.time('for'); | |
for (let i = 0, l = ar.length; i < l; i++) { | |
ar[i]; | |
} | |
console.timeEnd('for'); | |
console.time('for...in'); | |
for(let i in ar) { | |
ar[i]; | |
} | |
console.timeEnd('for...in'); | |
console.time('for...of'); | |
for(let v of ar) { | |
v; | |
} | |
console.timeEnd('for...of'); | |
console.time('for...of and Array.entries'); | |
for(let pair of ar.entries()) { | |
pair[1]; | |
} | |
console.timeEnd('for...of and Array.entries'); | |
console.time('Array.forEach'); | |
ar.forEach(function(v){ | |
v; | |
}); | |
console.timeEnd('Array.forEach'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// テスト用オブジェクトの作成 | |
let obj = {}; | |
for (let i = 0; i < 100; i++) { | |
obj[`test${i}`] = "test"; | |
} | |
console.time('for...in'); | |
for (let k in obj) { | |
obj[k]; | |
} | |
console.timeEnd('for...in'); | |
console.time('for...of and Object.keys'); | |
for(let k of Object.keys(obj)) { | |
obj[k]; | |
} | |
console.timeEnd('for...of and Object.keys'); | |
console.time('Array.forEach and Object.keys'); | |
Object.keys(obj).forEach(function (k) { | |
obj[k]; | |
}); | |
console.timeEnd('Array.forEach and Object.keys'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for(let v of ar) { | |
console.log(v); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for(let [i, v] of ar.entries) { | |
console.log(v); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ar.forEach(function(v, i, a){ | |
console.log(v); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let obj = {test1: 10, test2:20, test3:30}; | |
for(let k in obj) { | |
console.log(obj[k]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for(let k of Object.keys(obj)) { | |
console.log(obj[k]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object.keys(obj).forEach(function (k) { | |
console.log(obj[k]); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let ar = [10, 20, 30]; | |
// prototype拡張 | |
Array.prototype.extendTest = "prototype拡張"; | |
// Arrayオブジェクトにプロパティ追加 | |
ar.test = "test"; | |
console.log("for"); | |
for(let i = 0; i < ar.length; i++) { | |
console.log(ar[i]); | |
} | |
console.log("-----------------"); | |
console.log("for...in"); | |
for(let i in ar) { | |
console.log(ar[i]); | |
} | |
console.log("-----------------"); | |
console.log("for...of"); | |
for(let v of ar) { | |
console.log(v); | |
} | |
console.log("-----------------"); | |
console.log("for-of entries"); | |
for(let [k, v] of ar.entries()) { | |
console.log(v); | |
} | |
console.log("-----------------"); | |
console.log("Array.forEach"); | |
ar.forEach(function(v){ | |
console.log(v); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for | |
10 | |
20 | |
30 | |
----------------- | |
for...in | |
10 | |
20 | |
30 | |
test | |
prototype拡張 | |
----------------- | |
for...of | |
10 | |
20 | |
30 | |
----------------- | |
for...of entries | |
10 | |
20 | |
30 | |
----------------- | |
Array.forEach | |
10 | |
20 | |
30 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment