0

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 ?????

9
  • 1
    Why would you want to do this? Think of the amount of memory you are trying to allocate in one go - this is why it is crashing. If you can expand on the why behind this requirement, we may be able to help. Commented Dec 19, 2013 at 11:27
  • What do you need to store that range for? Maybe whoever told you this meant range = {start: -15e9, end:15e9}? Commented Dec 19, 2013 at 11:29
  • 1
    Memory amount 30*10^9 * 8 bytes... 2.4*10^11 bytes. This is quite a lot for a web page :) Commented Dec 19, 2013 at 11:29
  • Ok. I am going to build a timeline with this range using div of 900px. So when user zoom in the div, she can view numbers in between the range. Anyhow I've to accommodate this range is the main requirement.-@Paddy Commented Dec 19, 2013 at 11:30
  • How about you then calculate a new, appropriate range whenever the user zooms. Commented Dec 19, 2013 at 11:31

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.