this python snippet was a fun read:
http://code.activestate.com/recipes/576564-walkers-alias-method-for-random-objects-with-diffe/
(also here in ruby)
this python snippet was a fun read:
http://code.activestate.com/recipes/576564-walkers-alias-method-for-random-objects-with-diffe/
(also here in ruby)
given the following function:
function addInts(anArray){ var result = 0; for (var i = 0; i < anArray.length; ++i){ if (anArray[i] < 21) { result = result + anArray[i]; } else { result = result + anArray[i] - 21; } } return result; }
what would you expect will run faster?
addInts(sortedArray); #sorted addInts(unsortedArray); #unsorted
[More programming riddles]
if you had to guess, which of the following two bits of javascript code would you say executes faster?
Exhibit A:
var keys = Object.keys(MyObj); //cloning the key array var length = keys.length; for (var i = 0; i < length; i++) { var val = MyObj[keys[i]]; };
Exhibit B:
for (var key in MyObj) { if (MyObj.hasOwnProperty(key)) { //hasOwnProperty is a native function var val = MyObj[key]; } }
[More programming riddles]
this is what things looked like last year in september, with an up to date version of chrome: