Replies: 5 comments
-
Essa dica é antiga. Hoje as engines de JS já cuidam dessa otimização pra gente :) |
Beta Was this translation helpful? Give feedback.
-
@fernandofleury Bacana!! Então se rodar no IE 8 ou 9 vai rolar uma diferença? |
Beta Was this translation helpful? Give feedback.
-
@lagden No 9 eu não sei, mas no 8 com certeza. |
Beta Was this translation helpful? Give feedback.
-
@fernandofleury testei no IE 11 É agressivo mesmo!!! Quase a metade do tempo!! // IE com cache
function for1IE() {
'use strict';
var a = [];
for (var _i = 0; _i < 9999999; _i++) {
a.push(_i);
}
var before = new Date();
for (var c = 0; c < 1e3; c++) {
// ...
}
for (var _i2 = 0, _len = a.length; _i2 < _len; _i2++) {
// ...
}
var after = new Date();
console.log(after.getTime() - before.getTime());
}
for1IE(); // IE sem cache
function for2IE() {
'use strict';
var a = [];
for (var _i = 0; _i < 9999999; _i++) {
a.push(_i);
}
var before = new Date();
for (var c = 0; c < 1e3; c++) {
// ...
}
for (var _i2 = 0; _i2 < a.length; _i2++) {
// ...
}
var after = new Date();
console.log(after.getTime() - before.getTime());
}
for2IE(); |
Beta Was this translation helpful? Give feedback.
-
@lagden coloca o resultado do teste com IE11 pra gente!! =D |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hoje esta lendo sobre dicas de javascript e estava falando de cachear o
length
doarray
antes de chamar o loopfor
...Logo abaixo nos comentários um rapaz disse:
Bullshit
Fiquei na dúvida e fiz um benchmark!!
E não era que o sujeito estava certo!!
https://gist.github.com/lagden/0ec5360c39e9a74783d6071a96c8c59e
for1.js
com cachefor2.js
sem cacheOu seja, com ou sem da na mesma!!
O que me dizem?
Beta Was this translation helpful? Give feedback.
All reactions