javascript speed test

Aside

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

 


 
check your answer


 

[More programming riddles]

javascript anagram detector

Aside

what’s the shortest anagram detector you know how to write, in javascript?


there must be a shorter way to do this. highlight below to see a solution.


var anagramDetector = function (x,y) {
   if (x.length != y.length) return false;
   var alpha_x = x.split("").sort().join("");
   var alpha_y = y.split("").sort().join("");
   for ( j = 0; j<y.length;++j) {
       if (alpha_x[j] != alpha_y[j]) return false;
  }
  return true;
}

PS: there’s a cool method for solving this for a fixed alphabet, using primes


[More programming riddles]

detecting anagrams in python

Aside

what’s the shortest anagram detector you know how to write, in python?


this is one that keeps feeling like it should be a one liner (because it’s very similar to this one liner), but i suspect can’t be done in one line

highlight to see a solution.


def fn(x,y):
    if len(x) != len(y):
        return False
    for i in x:
        y = y.replace(i,'',1)
    return not len(y) > 0

PS: there’s a cool method for solving this for a fixed alphabet, using primes


[More programming riddles]

javascript speed test

Aside

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];
 }
}

Check your answer.


[More programming riddles]

 

 

 


this is what things looked like last year in september, with an up to date version of chrome:

Screen Shot 2013-09-17 at 7.04.32 PM

whyfore chat, with django, twisted and websockets?

Aside

upon seeing the work i’ve put into writing tutorials, showing how to get realtime chat working in django + twisted/websockets, you might make the assumption that i consider this architecture to be, in general, a good idea.

A.

twisted’s implementation of websockets is, as of this writing, not integrated into the main branch.

don’t use code that isn’t considered, by its authors, to be reliable enough to merge into and release as part of their application distribution.

B.

twisted is an event-driven networking engine
django is a solid, easy to use web framework
websockets, a tcp based protocol, is usually implemented as a strange mix between the tcp and http protocols

it is, generally speaking, not a good idea to mix abstraction levels; adding event-driven components to your application by combining twisted and django is a bad architectural decision. I strongly suggest you consider using twisted.web instead of mixing django and twisted.

websockets are a strange mix of protocols, and can be difficult to work with unless you are very careful with your choice of libraries and application design, scope and implementation. at the time of this post, i would recommend against using websockets, in production, with the standard deployment of twisted. i strongly urge you to consider the following alternatives, in rough order of likelihood to work for you: