最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

What is a quick way to determine if an array is only posed 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 (){ 

}

What is a quick way to determine if an array is only posed 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 (){ 

}
Share Improve this question asked Apr 9, 2018 at 23:58 William MerrittWilliam Merritt 4391 gold badge5 silver badges13 bronze badges 7
  • 3 array.every(v => v === 0 || v === 1) – Jaromanda X Commented Apr 9, 2018 at 23:58
  • note ... true=1 and false=0 is more logical ... especially since +true === 1 and +false === 0 – Jaromanda X Commented Apr 10, 2018 at 0:02
  • Faster way would be to use a for loop. – ibrahim mahrir Commented Apr 10, 2018 at 0:02
  • yes, because using .every on an 8 length array is so much slower than a for loop ... nanoseconds are precious ... array.some(v => v !== 0 && v !== 1); better? – Jaromanda X Commented Apr 10, 2018 at 0:03
  • 2 @ibrahimmahrir - I see why you say a for loop is faster - because Chrome native array methods (some, every, etc) are pitifully slow – Jaromanda X Commented Apr 10, 2018 at 0:28
 |  Show 2 more ments

3 Answers 3

Reset to default 3

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

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
}

https://jsperf./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 ment. 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);
发布评论

评论列表(0)

  1. 暂无评论