0

How many operations are there in a Javascript Array Join method?

Is it simply one operation, or is the number of operations the same as the number of items in the array?

The reason I ask is because I want to know what is the cheapest way to compare the two arrays below:

var a = ["Apples", "Pears", "Bananas"]
var b = ["Apples", "Oranges", "Bananas"]

Method A

For (var i = 0; i < 2; i++) {
  if (a[i] === b[i]) console.log(false);
}

Method B

aa = a.join("&");
bb = b.join("&");
if (aa === bb) console.log(false)

Cheapest way without just writing a === b, because the point is comparing the values in two different arrays.

Dol
  • 944
  • 3
  • 10
  • 25
  • 3
    If by "number of operations", you mean its "big O" behavior, of course it's not O(1); it's at least O(n) and quite possibly worse. In addition to its performance (probably not that good), there are other reasons not to use `join`. For instance, your `join` approach would report that `['a&b', 'c']` is equal to `['a', 'b&c']`. It is very unlikely that there is any faster alternative than to loop through the arrays. Just our of curiosity, whence comes your focus on efficiency here? –  Nov 01 '15 at 11:38
  • 2
    Join might be "cheapest" but definitely worst, as it simply won't work well. Example: `[1,2,3].join('&&') === ['1', 2, 3].join('&&') // true` although `"1" === 1 // => false`. Not even to mention that you will not be able to compare arrays of non-primitives. – dfsq Nov 01 '15 at 11:38
  • 1
    a[i] === b[i] in method A will compare not array items, it will compare pointers in memory. Use == – ivan Nov 01 '15 at 11:38
  • See http://stackoverflow.com/questions/3115982/how-to-check-if-two-arrays-are-equal-with-javascript, and http://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript. –  Nov 01 '15 at 11:46
  • Deep compare or shallow compare? –  Nov 01 '15 at 11:48
  • Is your ultimate question whether `join` is a single operation, whatever that means, or is it what the cheapest way to do a shallow array comparison is? –  Nov 01 '15 at 11:54
  • The ultimate question is how many operations are there in `join`, its less about comparing arrays. Thanks for the links. – Dol Nov 01 '15 at 12:01

2 Answers2

4

The Array.prototype.join (separator) of the ECMAScript 2015 (6th Edition, ECMA-262) performs the following steps:

Let O be ToObject(this value).
ReturnIfAbrupt(O).
Let len be ToLength(Get(O, "length")).
ReturnIfAbrupt(len).
If separator is undefined, let separator be the single-element String ",".
Let sep be ToString(separator).
ReturnIfAbrupt(sep).
If len is zero, return the empty String.
Let element0 be Get(O, "0").
If element0 is undefined or null, let R be the empty String; otherwise, let R be ToString(element0).
ReturnIfAbrupt(R).
Let k be 1.
Repeat, while k < len
    Let S be the String value produced by concatenating R and sep.
    Let element be Get(O, ToString(k)).
    If element is undefined or null, let next be the empty String; otherwise, let next be ToString(element).
    ReturnIfAbrupt(next).
    Let R be a String value produced by concatenating S and next.
    Increase k by 1.
Return R.

Notes:

  • The elements of the array are converted to Strings, and these Strings are then concatenated, separated by occurrences of the separator. If no separator is provided, a single comma is used as the separator.
  • The join function is intentionally generic; it does not require that its this value be an Array object. Therefore, it can be transferred to other kinds of objects for use as a method.

I would say your first method is cheaper :) But you can run your own benchmarks.

Laurentiu L.
  • 6,566
  • 1
  • 34
  • 60
1

Almost by definition, there is nothing faster than the following, assuming you want to do a shallow compare.

function equal(a, b) {
  if (a === b) return true;
  if (a.length !== b.length) return false;
  for (var i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
  return true;
}

A quick test shows that this is 50 times faster than using join or stringify.