JSFiddle - React, Tailwind, and code Playground
by gutek
JavaScript
var foo = 1;
function slow (count) {
var i,
inner = function () {
function moreInner() {
for (i = 0; i < count; i += 1) {
console.log(foo);
}
}
moreInner();
}
inner();
}
function test (name, method) {
console.time(name);
console.group(name);
method(10000);
console.groupEnd(name);
console.timeEnd(name);
}
test ('slow access, deep scope', slow);
test ('fast acces, deep scope', ok);
function ok (count) {
var i, f = foo,
inner = function () {
function moreInner() {
for (i = 0; i < count; i += 1) {
console.log(f);
}
}
moreInner();
}
inner();
}
test ('best way, shallow scope', best);
function best (count) {
var i, f = foo;
for (i = 0; i < count; i += 1) {
console.log(f);
}
}