0

I'm trying to post an array that gets modified in a local web page of 672 elements back to the server. For this I put it in a string, seperated by comma's, in a Javascript like this:

    alert("begin");
    var sBsbData=""
    for (var x=0;x<24*4*7;x++)
        sBsbData = sBsbData + BsbData[x] + ',';

    alert(sBsbData);

BsbData is an array of int's; values don't exceed 10.

This is code that would get processed by any processor without a blink of an eye... yet it takes about ten seconds between the two alerts! What am I doing wrong here?? Did I pick a particularly bad concat method for this purpose?

Jur
  • 92
  • 6
  • 1
    Is there a specific reason you are not using `BsbData.join(',')`? But aside from that, the loop doesn't seem to take up any measurable time: http://jsfiddle.net/AUCWS/. – Felix Kling Sep 06 '13 at 10:02
  • Why don't you just use `var sBsbData = BsbData.join(',');`? – h2ooooooo Sep 06 '13 at 10:02
  • Did you try serializing the array into a string? You can simply write `var sBsbData = BsbData.toString()`, and the format will be the same (values separated by commas). – Frédéric Hamidi Sep 06 '13 at 10:03
  • I know using `++x` is slightly faster, but I don't think that's the problem. – Romain Braun Sep 06 '13 at 10:04
  • @RomainBraun there is actually no difference on non-braindead engines like V8 and SpiderMonkey. – Esailija Sep 17 '13 at 09:41

1 Answers1

3

It is not slow - it's the alert that takes a while to be created (for some odd reason).

Proof of concept:

var BsbData = [];
for (var i = 0; i < 24 * 4 * 7; i++) {
    BsbData[i] = Math.round(Math.random() * 10);
}

console.log("begin");
alert("begin");

var sBsbData=""
for (var x=0;x<24*4*7;x++)
    sBsbData = sBsbData + BsbData[x] + ',';

console.log(sBsbData);
// !!! This is where the code will halt for a bit (the string has already been created) !!!
alert(sBsbData);

That said - you should just use var sBsbData = BsbData.join(',');

h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
  • Note that passing `,` to `join()` gives the exact same result as invoking `toString()` on the array, and may actually be slower. – Frédéric Hamidi Sep 06 '13 at 10:10
  • @FrédéricHamidi Does the JS standard specify how an array should be returned if invoking `toString`, or could you risk having different results in different browsers? (eg `1,2,3` in Chrome, but `1, 2, 3` in IE?) – h2ooooooo Sep 06 '13 at 10:11
  • That's part of the spec, so the results will be consistent across browsers. I'll try to find a link. – Frédéric Hamidi Sep 06 '13 at 10:12
  • @Felix, `join()` will be faster then. That's a pity, `toString()` looks more like a primitive operation than `join()` :) – Frédéric Hamidi Sep 06 '13 at 10:14
  • @Felix, strange, on second reading the ES5 spec does not mention passing the comma character to `join()` (it says to pass no argument). `Object.prototype.toString()` can also be invoked if `join()` is not defined. Seems more complicated than it looks. – Frédéric Hamidi Sep 06 '13 at 10:18
  • Thanks guys, yeah, the problem was in the alert itself... will remember not to test performance with alerts in the future! Oh and I didn't use either join or toString because I didn't know they existed... I do now! – Jur Sep 06 '13 at 10:23
  • 1
    @Frédéric: I think the `.join` method is defined such that if no argument is passed, `,` is used as separator. – Felix Kling Sep 06 '13 at 10:25