Valay
Valay

Reputation: 1997

javascript - store large range of numbers in array

Please find below the code:

var range = new Array();
var start = -15e9;
var end = 15e9;
for(var i=start; i<end; i++){
    range.push(i);
}

When I run this code in jsfiddle or in a browser, it gets crashed. Here the requirement is to store the range of -15x10^9 to 15x10^9.

What is the best way (performance-wise) to store such a large range in javascript ?????

Upvotes: 0

Views: 640

Answers (1)

Paweł Obrok
Paweł Obrok

Reputation: 23174

You have to ask yourself what kind of operations you need for your range. If for example you only want to check if a number is in the range you can do something like:

function range(lo, hi) {
  return function(number) {
    return (number >= lo) && (number <= hi);
  }
}

var r1 = range(-15e9, 15e9);
r1(0); // true

Upvotes: 1

Related Questions