简体   繁体   English

JavaScript中的布尔值数组

[英]Array of boolean values in JavaScript

In JavaScript, is there a way to do this in a more efficient way? 在JavaScript中,有没有一种方法可以更有效地做到这一点?

I need to create an array of boolean values, change them and check them individually and at random. 我需要创建一个布尔值数组,更改它们并分别并随机检查它们。

The goal is better performance. 目标是更好的性能。 Maybe manipulating bits. 也许操纵位。

Right now I use something like this: 现在我用这样的东西:

var boolean = [];

var length = 100; // set a random number of values
for (var i = 0; i < length; i++) boolean[i] = false; // or true

boolean[n] = true; // change some of the values randomly
if (boolean[n]) { /* something */ } // check some of the values randomly

So there are three parts to this: 因此,这包括三个部分:

  1. Creating the array 创建数组

    Somewhat counter-intuitively, what you're doing is already just fine even though standard JavaScript arrays aren't really arrays at all , because the way you're creating and filling in the array, a modern engine will use a true array behind the scenes. 有点违反直觉的是,即使标准JavaScript数组根本不是真正的数组 ,您所做的也已经足够好了 ,因为在创建和填充数组的方式中,现代引擎将在场景。 (See my answer to this other question for more on that, including performance tests.) So even on engines that have true arrays like Uint8Array , what you're doing is fine. (有关此问题的更多信息,请参见我对另一个问题的回答 ,包括性能测试。)因此,即使在具有真正数组(例如Uint8Array )的引擎上,您所做的也很好。 But see point #2 below. 但是请参阅下面的第二点。

  2. Filling it with falsey values 用假值填充它

    As there are only 100 entries, it just doesn't matter how you do this, unless you're creating and filling in the array repeatedly in a tight loop. 因为只有100个条目,所以执行此操作无关紧要,除非您要在紧密的循环中重复创建和填充数组。 If you are, then a Uint8Array should win, because new Uint8Array(100) is pre-filled with zeroes and you don't have to fill it in at all. 如果是的话,那么Uint8Array应该会获胜,因为new Uint8Array(100)会预先填充零,而您根本不必填写。

  3. Accessing the array's entries 访问数组的条目

    You don't really have much choice there, you do it the way you're doing it. 您实际上没有太多选择,可以按照自己的方式来做。 Provided you create the array the way you are or you use a Uint8Array , that's probably as fast as it's going to get. 如果您以自己的方式创建数组,或者使用Uint8Array ,那么它的速度可能会与所获得的一样快。

I find http://jsperf.com helpful for comparing approaches to things and seeing how they turn out on real-world JavaScript engines. 我发现http://jsperf.com对于比较事物的方法以及在现实世界的JavaScript引擎上观察它们的效果很有帮助。 For instance, here's a test case suggesting that a Uint8Array will offer a slight advantage on SpiderMonkey (Firefox's engine), be about the same on V8 (Chrome's engine), and very slightly slower on JScript (IE11's engine): 举例来说,这里是一个测试用例这表明Uint8Array将提供一个微弱的优势上的SpiderMonkey(Firefox的引擎),是关于V8(Chrome的引擎)是相同的,并且非常稍微慢一点上的JScript(IE11的引擎):

Standard array: 标准阵列:

var a, n, dead;

// Creation
a = [];

// Filling
for (n = 0; n < 100; ++n) {
    a[n] = false;
}

// Accessing randomly 5,000 times
dead = 1;
for (n = 0; n < 5000; ++n) {
    a[Math.floor(Math.random() * a.length)] = true;
    if (a[Math.floor(Math.random() * a.length)]) {
        ++dead; // Just to be doing something
    }
}

// Make sure engine knows we're using the result
if (dead === 0) { throw "Error in test"; }

Uint8Array : Uint8Array

var a, n, dead;

// Creation
a = new Uint8Array(100);

// Filling
// None!

// Accessing randomly 5,000 times
dead = 1;
for (n = 0; n < 5000; ++n) {
    a[Math.floor(Math.random() * a.length)] = 1;
    if (a[Math.floor(Math.random() * a.length)]) {
        ++dead; // Just to be doing something
    }
}

// Make sure engine knows we're using the result
if (dead === 0) { throw "Error in test"; }

Results on Chrome, Firefox, and IE11: 在Chrome,Firefox和IE11上的结果:

在此处输入图片说明

may be this way 可能是这样

var length = 100,
    item,
    arr = (Array(length)).fill(1),
    getRandBool = function() { return Math.random(Date()) * 10 > 6; },
    getRandIdx = function() { return (Math.random(Date()) * (length + 1))|0; };

arr.forEach(function(val, idx) { arr[idx]= getRandBool(); });

arr[getRandIdx()] = getRandBool();


if(item = arr[getRandIdx()]) { console.log(item) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM