Fastest way to check if all elements in an array are equal in JavaScript

Here are four algorithms and performance metrics below:

function allAreEqual(array){
    if(!array.length) return true;
    // I also made sure it works with [false, false] array
    return array.reduce(function(a, b){return (a === b)?a:(!b);}) === array[0];
}
function same(a) {
    if (!a.length) return true;
    return a.filter(function (e) {
        return e !== a[0];
    }).length == 0;
}

function allTheSame(array) {
    var first = array[0];
    return array.every(function(element) {
        return element === first;
    });
}

function useSome(array){
    return !array.some(function(value, index, array){
        return value !== array[0];
    });
}

Results:

allAreEqual x 47,565 ops/sec ±0.16% (100 runs sampled)
same x 42,529 ops/sec ±1.74% (92 runs sampled)
allTheSame x 66,437 ops/sec ±0.45% (102 runs sampled)
useSome x 70,102 ops/sec ±0.27% (100 runs sampled)

So apparently using builtin array.some() is the fastest method of the ones sampled.

Node JS scalable programming

I have been doing research on how to utilize many machines to complete a task faster using node js. It is a rendering project that I’m doing where I need to render several hundred images in good quality. So obviously the way to do it is to divide up the task into smaller pieces, so that’s exactly what I decided to do.

For all kinds of parallel programming I prefer using Node JS, because of it’s async model and the ease with which you can run code remotely. JavaScript can be easily stringified like this: var foo = funciton (){}; foo.toString(). And then sent over the network. So this way you can easily send cpu intensive work tasks to other machines and have them execute remotely and then return the result.

As long as you have a machine that has SSH installed, it is fairly easy to use node js ssh2 module and ssh to each of the worker machines, run a script remotely and then combine the results into the final work. In this case it was blender files that had to be rendered. And since blender is scriptable in python, it is also fairly easy to automate the whole render process.

So the way to distribute a render task to a cluster of computers is as follows:

  • write python script that takes render parameters such as frame to render etc. this script will be invoked from javascript
  • write a node js program that ssh:s into each of the machines and fires up a blender process rendering a fragment of the work
  • when the blender process completes, grab the result and store it on the master node.
  • repeat.

 

Write server side and client side code in a single file (Node JS)

In siteboot you can now do things like this:

Server(function(){
        $.fn.my_control = function(){
                return this.each(function(){
                        $(this).html("<button>Get message</button>"); 
                }); 
        } 
        Server.registerCommand({
                name: "get-message",
                method: function(req, res){
                        var ret = req.server.defer(); 
                        ret.resolve({message: "YAY! It's working!"}); 
                        return ret.promise; 
                }
        }); 
}); 

Client(function(){
        $.fn.my_control = function(){
                return this.each(function(){
                        var self = $(this); 
                        self.click(function(){
                                server.exec("get-message", []).done(function(data){
                                        self.html(data.message); 
                                }); 
                                return false; 
                        }); 
                }); 
        }
        $(document).ready(function(){
                $(".my_control").my_control();
        });
}); 

Very simple and concise way to write a widget. Client code will run inside the user browser and server code will handle the server side requests.

SiteBoot is a node js web server framework being developed on GitHub: https://github.com/mkschreder/siteboot.js