본문 바로가기
카테고리 없음

[JavaScript] for loop 성능 테스트

by 혀나Lee 2019. 12. 4.

성능 테스트: http://jsben.ch/fNiQv

 

JSBEN.CH Benchmarking for JavaScript -

var arr = [1, 2, 4 , 5, 66, 38, 39, 3993, 33, "test", "hello", 93, 93, 20,     77, "hu", 92, 18, 4 , 5, 66, 38, 39, 3993, 33, "test", "hello", 93, 93     , 20, 77, "hu", 92, 18, 4 , 5, 66, 38, 39, 3993, 33, "test", "hello",     93, 93, 20, 77, "hu", 92, 18

jsben.ch

예제

/*use this block to define your objects and
 stuff which has to be done for every test block
for example: 'var testObject = {a: 3, b: 2}'*/
var arr = [1, 2, 4 , 5, 66, 38, 39, 3993, 33, "test", "hello", 93, 93, 20, 77, "hu", 92, 18, 4 , 5, 66, 38, 39, 3993, 33, "test", "hello", 93, 93, 20, 77, "hu", 92, 18, 4 , 5, 66, 38, 39, 3993, 33, "test", "hello", 93, 93, 20, 77, "hu", 92, 18, 4 , 5, 66, 38, 39, 3993, 33, "test", "hello", 93, 93, 20, 77, "hu", 92, 18, 4 , 5, 66, 38, 39, 3993, 33, "test", "hello", 93, 93, 20, 77, "hu", 92, 18, 4 , 5, 66, 38, 39, 3993, 33, "test", "hello", 93, 93, 20, 77, "hu", 92, 18];
var dosmth;

for loop - length uncached

for (var x = 0; x < arr.length; x++) {
    dosmth = arr[x];
}

for loop - cached length

var l = arr.length;
for (var x = 0; x < l; x++) {
    dosmth = arr[x];
}

for ... in

for (var x in arr) {
    dosmth = arr[x];
}

for ... of

for (var a of arr) {
    dosmth = a;
}

forEach

arr.forEach(a => {
   dosmth = a; 
});

 

댓글