Does JavaScript have a method like “range()” to generate a range within the supplied bounds?

后端 未结 30 3242
广开言路
广开言路 2020-11-22 00:51

In PHP, you can do...

range(1, 3); // Array(1, 2, 3)
range(\"A\", \"C\"); // Array(\"A\", \"B\", \"C\")

That is, there is a function that l

30条回答
  •  萌比男神i
    2020-11-22 01:41

    Did some research on some various Range Functions. Checkout the jsperf comparison of the different ways to do these functions. Certainly not a perfect or exhaustive list, but should help :)

    The Winner is...

    function range(lowEnd,highEnd){
        var arr = [],
        c = highEnd - lowEnd + 1;
        while ( c-- ) {
            arr[c] = highEnd--
        }
        return arr;
    }
    range(0,31);
    

    Technically its not the fastest on firefox, but crazy speed difference (imho) on chrome makes up for it.

    Also interesting observation is how much faster chrome is with these array functions than firefox. Chrome is at least 4 or 5 times faster.

提交回复
热议问题