Benchmarking javascript inserting a new element into a sorted array – three approaches




Task: having a sorted array add one value so that the array remains sorted in browser-based JavaScript.

Assumption: internal array sort may win to some extent over interpreted search and insert.

What I did is implemented all three algorithms and made a post to demonstrate the results of the benchmarking.

Tests are run in async mode to allow Google Charts do their work and prevent browser freezing. Sync mode will give more accurate results, though.

Each method is run several times against a starting array of the same size. On next benchmark cycle the size of array doubles until the maxSize limit is reached.

The benchmarking library only takes into account the wall time. Though linear is faster on low array sizes (less than 64 in my case), it is more CPU intensive.

Feel free to run runTest(size) in console. Bear in mind that the chart may break if size is less than previous displayed.

You can set maxSize param in url. For 65536 use this link

Results: Internal Array.sort() shouldn’t be preferred over logarithmic or even linear search for all cases. Logarithmic search also has lower CPU usage. No wonder, though. Use logarithmic FTW.

The code

1
2
3
4
5
6
7
8
9
10
11
          /*
           *  A simplest method that comes at the first sight.
           *  O(1) JavaScript interpreted insert and then the sort
           *  that is probably implemented as a precompiled method
           *  (or not?), but should take at least O(n*ln(n))
           */

            Array.prototype.addSorted1 = function(element){
                this.push(element);
                this.sort();
                return this;
            }

vs

1
2
3
4
5
6
7
8
9
          /*
           *  An O(n) JavaScript interpreted new element inserter
           */

            Array.prototype.addSorted2 =  function(element){
                for(var i=0;i<;this.length && this[i] < element;i++){
                }
                this.splice(i,0,element);
                return this;
            }

vs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
          /*
           *  An O(ln(n)) JavaScript interpreted new element inserter
           */

            Array.prototype.addSorted3 = function(element){
                var i=0;
                var j=this.length;
                var h;
                var c=false;
                if (element>this[j]){
                    this.push(element);
                    return this;
                }
                if (element<this[i]){
                    this.splice(i,0,element);
                    return this;
                }
                while(c==false){
                    h=~~((i+j)/2); //a faster h=Math.floor((i+j)/2);
                    if (element>this[h]){
                        i=h;
                    } else {
                        j=h;
                    }
                    if (j-i<=1){
                        this.splice(j,0,element);
                        c=true;
                    }
                }
                return this;
            };

The benchmark

After starting the benchmark, please wait for a minute or two… or check your console for errors 🙂
Logs: