7

I'm porting some of my OpenGL code to WebGL and the fact that JavaScript doesn't have genuine arrays is sad. I can use Float32Array's (and other other ArrayBuffer types), but that doesn't seem to help performance.

As an experiment to compare Array vs Float32Array vs Float64Array performance, I timed bubble sort on 100000 floats to see if there was any difference:

function bubbleSort(array) {
    var N = array.length;
    for (var i = 0; i < N; i++) 
        for (var j = i; j < N-1; j++)
            if (array[j] > array[j+1]) {
                var tmp = array[j];
                array[j] = array[j+1];
                array[j+1] = tmp;
            }
}

// var nums = new Array(100000);        // regular 'JS' array
// var nums = new Float32Array(100000);   // actual buffer of 32-bit floats
var nums = new Float64Array(100000);   // actual buffer of 64-bit floats
for (var i = 0; i < nums.length; i++)
    nums[i] = Math.random() * 1000;

bubbleSort(nums);

for (var i = 0; i < nums.length; i++)
    console.log(nums[i]);

Not much difference. Really the compiler would need some static type information for the array argument for bubbleSort to really get decent performance. Are we just stuck with bad array performance in JS? Any way around this? Short of using ASM.js that is...

9
  • 4
    How did you actually test this? Are you sure it's not the Math.random() being called 100k times that's causing your performance issues?
    – Rob
    Commented Oct 5, 2014 at 3:52
  • 1
    @Rob Remove bubbleSort and its finishes immediately.
    – wcochran
    Commented Oct 5, 2014 at 4:10
  • 4
    A post with "bubble sort" and "performance" seems an oxymoron.
    – user949300
    Commented Oct 5, 2014 at 4:36
  • [].sort.call(nums, function(a,b){return a-b}); is WAY faster
    – dandavis
    Commented Oct 5, 2014 at 4:49
  • 2
    I am amazed at folks who are missing the point. It was suppose to be slow! I just wanted to compare JS arrays vs typed arrays. I could have just done a 100 million random swaps, but I wanted the time to be mostly array indexing...
    – wcochran
    Commented Oct 5, 2014 at 5:04

1 Answer 1

6

You should see this answer: What is the performance of Objects/Arrays in JavaScript? (specifically for Google V8)

In particular the following points:

  • Array.push( data ); is faster than Array[nextIndex] = data by almost 20 times over.
  • V8 array writes are slightly faster than V8 reads

So yes, it looks like Array indexing is slow in JS. If it's true that array writes are faster than reads in performance test, then it is likely that the dynamic allocation isn't picky about where it puts the data (fastest first?), whereas when it comes to reading, that will be a lot of memory addresses to jump between and so will be far slower.

I'd be interested to see if you declare an array using literal syntax [0, 1, 2, ..., 100000], would the performance be any better?

(I'll set up a JSPerf if I have the time).

1
  • 1
    Excellent -- thanks! I imagine some (early) implementations just hashed on the index which is treated like a string -- then of course delete should be fast then. I was testing w/ Node which uses V8. More testing....
    – wcochran
    Commented Oct 5, 2014 at 13:48

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.