William Merritt
William Merritt

Reputation: 439

Fast way to check if a javascript array is binary (contains only 0 and 1)

What is a quick way to determine if an array is only composed of 0's and 1's?

I'm vaguely familiar with a boolean solution but am not away how to implement it. In order to implement is you would have to assign values to the boolean

true = 0 

and

false = 1 

But how would you implement this if you are given an array such that

array = [1,1,1,1,0,0,0,0]

isArrayBool (){ 

}

Upvotes: 1

Views: 4805

Answers (3)

Matt Way
Matt Way

Reputation: 33171

So I threw a few functions together in jsperf to test. The fastest one I found so far is the one below (which is much faster than the for of version):

function isBoolFor(arr) {
  for(var i=arr.length-1; i>=0; --i){
    if(arr[i] !== 0 && arr[i] !== 1) return false
  }
  return true
}

Comparison is here


EDIT: I played around with another version that turned out to be quicker:

function isBoolFor(arr) {
  for(var i=0; arr[i] === 0 || arr[i] === 1; i++){}
  return i === arr.length
}

The key here is that because most of the array you are checking will consist of zeros and ones, you can avoid doing two boolean checks every iteration by using the || instead of the && negative check. It is only a subtle improvement, and could be argued to be no better than the one above in practicality.


UPDATE: So the difference between all the for and while variants is too subtle to declare an overall winner. So in that case I would go for readability. If you want binary use typed arrays!


FINAL UPDATE:

I was curious and wanted to find an even faster version, and it can be done if the array is known to be only numbers. If the array contains only numbers, then you can use a bitwise operator check for either of the values in question. For example:

function isBoolFor(arr) {
  const test = ~0x01
  for(var i=arr.length-1; i>=0; --i){
    if(test & arr[i]){ return false }
  }
  return true
}

Upvotes: 2

Nerdi.org
Nerdi.org

Reputation: 923

https://jsperf.com/bool-array-test-2/1

As we can see here, the fastest way to do this can be seen here... With a simple for loop, as suggested by user Jaromanda in a comment. This for loop blows other solutions out of the water in terms of speed.

 var someArray = [1,0,1,1,1,1,0,0,1,0,1,0,0,1,0,1,0,1,1,1,1,0,1,1,0,1,0]; 

 function simpleForLoop(array){
   var numArgs = someArray.length;
   for(var loop = 0; loop < numArgs; loop++){
    var arg = someArray[loop];
    if(arg  !== 0 && arg !== 1){ return false; }
   }
   return true;
  }

  var isbool = simpleForLoop(someArray);

Upvotes: 0

Daniele Cappuccio
Daniele Cappuccio

Reputation: 2192

Very very naive solution:

function isArrayBool(array) {
    for (var i of array) {
         if (i !== 0 && i !== 1) return false;
    }
    return true;
}

console.log(isArrayBool([1,0,0,0,1]));   // true
console.log(isArrayBool([1]));           // true
console.log(isArrayBool([2,3,0]));       // false

Upvotes: 3

Related Questions