JSFiddle - React, Tailwind, and code Playground

by Drath

JavaScript

var badGlobal = {
  thing: 3,
  otherThing: 3019293,
  parentThing: {
    subThing: "test"
  }
}

// GLOBAL

function globalFunction() {
  var goodLocal = badGlobal;
  var test = badGlobal.thing + badGlobal.otherThing;
  var test2 = "test " + badGlobal.parentThing.subThing;
}

var t0 = performance.now();
for (var i = 0; i <= 100000; i++) {
	globalFunction();
}
var t1 = performance.now();
console.log("Global took " + (t1 - t0) + " milliseconds.");

// LOCAL
function localFunction(globalThing) {
    var goodLocal = globalThing;
    var test = goodLocal.thing + goodLocal.otherThing;
    var test2 = "test " + goodLocal.parentThing.subThing;
}

var t2 = performance.now();
for (var i = 0; i <= 100000; i++) {
	localFunction(badGlobal);
}
var t3 = performance.now();
console.log("Local took " + (t3 - t2) + " milliseconds.");