4 ms·
It's a good example of how coming from different language can both help and inhibit programmer's thinking. Knowing C certainly helps to understand that repetiti
by Unosolo 16y ago
It's a good example of how coming from different language can both help and inhibit programmer's thinking. Knowing C certainly helps to understand that repetitive array size and index access is likely to be more expensive than using local variables. At the same time C programmer is more likely to adopt an imperative programming style, which is in this case more error prone, verbose and doesn't allow for code re-use.
More ideomatic JavaScript would look like this:
Array.prototype.foreach = function (f) {
for (var i = 0, _i = this.length; i < _i; i++) f(this[i])
}
/** BEGIN **/
var f = new Array(3000000);
console.time(1);
f.foreach( function (el) {
if (el === 'foobar' ||
el === 'foobaz' ||
el === 'baz' ||
el === 'barfoo') // Do something
})
console.timeEnd(1);
/** END **/