Like in any language that is weakly typed, you can’t avoid the fact that performing comparisons across types will cost you CPU cycles.
Consider the following code which does a .filter on an array of 5M entries, all of which are Numbers:
let arrOfNumbers = Array(5000000).fill(1);
console.time('eqeq-number')
arrOfNumbers.filter(a => a == 1)
console.timeEnd('eqeq-number')
console.time('eqeqeq-number')
arrOfNumbers.filter(a => a === 1)
console.timeEnd('eqeqeq-number')
On my Mac, they’re roughly equivalent, with a marginal difference in the performance in the eqeq and eqeqeq case:
eqeq-number: 219.409ms eqeqeq-number: 225.197ms
I would have assumed that the eqeqeq would have been faster given there’s no possibility of data type coercion, but it’s possible the VM knew everything was a number in the array and the test value, so, meh, about the same.
Now, for the worst case scenario, consider this following code: the same .filter, but the array is now full of 5M strings of the value “1”:
let arrOfStrings = Array(5000000).fill('1');
console.time('eqeq-string')
arrOfStrings.filter(a => a == 1)
console.timeEnd('eqeq-string')
console.time('eqeqeq-string')
arrOfStrings.filter(a => a === 1)
console.timeEnd('eqeqeq-string')
The eqeq costs about the same as the original example with the weakly typed Number to Number comparison, but now the eqeqeq is significantly faster:
eqeq-string: 258.572ms eqeqeq-string: 72.275ms
In this case it’s clear to see that the eqeqeq case doesn’t have to do any data coercion since the types don’t match, the evaluation is automatically false without having to muck the String to a Number. If you were to continue to mess around and have the .filters compare eqeq and eqeqeq to a String ‘1’ the results again are the same as the first few tests.
Conclusion? Same the VM work if you can. This is a really obtuse example as the eqeqeq can quickly shortcut the comparison to “false” since the types don’t match, but anywhere you can save effort when working on large data sets, it’s helpful to do so, and typing is an easy win when you can take it.
The very first thing my boss at Sencha asked me to work on when I joined was: “figure out what we’re doing with Designer”. Having known nothing then and only a skeleton team we started to sketch out what would become Sencha Architect 2. That was a year ago, and thankfully we’ve been lucky enough to be joined at Sencha by a great team from engineering, to product management, to UX that made Sencha Architect 2 happen. It’s crazy to look back to see in the last year all the amazing products we’ve released, such as Sencha Touch 2, the preview of Sencha.io, and now Architect 2. It’s such a huge step forward for us and for the web. I’ve been on the road demoing it at conferences and people go crazy when you see how easy it is to build an HTML5 app, which is a testament the work the team has put in to make the product easy and productive to use.