union array faster

JavaScript performance comparison

Test case created by alejandrolechuga

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
KennyTM
function union_arrays(x, y) {
  var obj = {};
  for (var i = x.length - 1; i >= 0; --i)
  obj[x[i]] = x[i];
  for (var i = y.length - 1; i >= 0; --i)
  obj[y[i]] = y[i];
  var res = []
  for (var k in obj) {
    if (obj.hasOwnProperty(k)) // <-- optional
    res.push(obj[k]);
  }
  return res;
}
union_arrays([34, 35, 45, 48, 49], [44, 55]);
pending…
alejandro
function unionArray(arrayA, arrayB) {
  var obj = {},
      i = arrayA.length,
      j = arrayB.length,
      newArray = [];
  while (i--) {
    if (!obj[arrayA[i]]) {
      obj[arrayA[i]] = true;
      newArray.push(arrayA[i]);
    }
  }
  while (j--) {
    if (!obj[arrayB[j]]) {
      obj[arrayB[j]] = true;
      newArray.push(arrayB[j]);
    }
  }
  return newArray;
}
unionArray([34, 35, 45, 48, 49], [44, 55]);
pending…
alejandro2
function unionArray(arrayA, arrayB) {
  var obj = {},
      i = arrayA.length,
      j = arrayB.length,
      newArray = [],
      max = Math.max(i, j);
  while (max--) {
    if (!obj[arrayA[max]] && i > max) {
      obj[arrayA[max]] = true;
      newArray.push(arrayA[max]);
    }
    if (!obj[arrayB[max]] && j > max) {
      obj[arrayB[max]] = true;
      newArray.push(arrayB[max]);
    }
  }
  return newArray;
}
unionArray([34, 35, 45, 48, 49], [44, 55]);
pending…
alejandro3
function unionArray(arrayA, arrayB) {
  var obj = {},
      i = arrayA.length,
      j = arrayB.length,
      newArray = [],
      max = Math.max(i, j);
  while (max--) {
    if (i > max && !obj[arrayA[max]]) {
      obj[arrayA[max]] = true;
      newArray.push(arrayA[max]);
    }

    if (j > max && !obj[arrayB[max]]) {
      obj[arrayB[max]] = true;
      newArray.push(arrayB[max]);
    }
  }
  return newArray;
}
unionArray([34, 35, 45, 48, 49], [44, 55]);
pending…
alejandro4
function unionArray(arrayA, arrayB) {
  var obj = {},
      i = arrayA.length,
      j = arrayB.length,
      newArray = [],
      max = Math.max(i, j);
  while (max--) {
    if (i > max) if (!obj[arrayA[max]]) {
      obj[arrayA[max]] = true;
      newArray.push(arrayA[max]);
    }
    if (j > max) if (!obj[arrayB[max]]) {
      obj[arrayB[max]] = true;
      newArray.push(arrayB[max]);
    }
  }
  return newArray;
}
unionArray([34, 35, 45, 48, 49], [44, 55]);
pending…
alejandro5
function unionArray(arrayA, arrayB) {
  var obj = {},
      i = arrayA.length,
      j = arrayB.length,
      newArray = [];
  while (i--) {
    if (!(arrayA[i] in obj)) {
      obj[arrayA[i]] = true;
      newArray.push(arrayA[i]);
    }
  }
  while (j--) {
    if (!(arrayB[j] in obj)) {
      obj[arrayB[j]] = true;
      newArray.push(arrayB[j]);
    }
  }
  return newArray;
}
unionArray([34, 35, 45, 48, 49], [44, 55]);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment