javascriptarraysnode.jsheap-memoryiccid

Nodejs / Javascript get the numbers between a start number and end number when each number has more than 15 digits


I was trying to get the numbers (iccid's - something reated to esim) between a start_number and end_number using the following logic :

let { start_iccid, end_iccid } = req.value.body;
let list = [];
let lowEnd = parseInt(start_iccid); // sample value : 100000000000000000
let highEnd = parseInt(end_iccid); // sample value : 100000000000000029
for (let i = lowEnd; i <= highEnd; i++) {
   list.push(i);
}

As expected i am getting the following errors :

FATAL ERROR: invalid array length Allocation failed - JavaScript heap out of memory
 1: node::Abort() [/home/cavli/.nvm/versions/node/v8.11.4/bin/node]
 2: 0x8c21ec [/home/cavli/.nvm/versions/node/v8.11.4/bin/node]
 3: v8::Utils::ReportOOMFailure(char const*, bool) [/home/cavli/.nvm/versions/node/v8.11.4/bin/node]
 4: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [/home/cavli/.nvm/versions/node/v8.11.4/bin/node]
 5: v8::internal::Heap::AllocateUninitializedFixedDoubleArray(int, v8::internal::PretenureFlag) [/home/cavli/.nvm/versions/node/v8.11.4/bin/node]
 6: v8::internal::Factory::NewFixedDoubleArray(int, v8::internal::PretenureFlag) [/home/cavli/.nvm/versions/node/v8.11.4/bin/node]
 7: 0xd554b5 [/home/cavli/.nvm/versions/node/v8.11.4/bin/node]
 8: v8::internal::Runtime_GrowArrayElements(int, v8::internal::Object**, v8::internal::Isolate*) [/home/cavli/.nvm/versions/node/v8.11.4/bin/node]
 9: 0x217d6df042fd

I understand that in Javascript, all numbers are IEEE double precision floating point numbers, which means that i only have about 16 digits of precision; the remainder of the 64 bits are reserved for the exponent. And i have to work some tricks to get more precision if you need all 64 bits.

I tried to use big-number javascript library but could not make it work with for loop .

What would be the best possible solution to this problem ?

I also tried to increase the nodejs memory limit after reading this thread but couldn't solve the issue : StackOverflow thread

Note : The biggest safe integer in JavaScript is 9007199254740991 (253-1), whereas the smallest safe integer is -9007199254740991 .

ICCID (Integrated Circuit Card Identifier) - A SIM card contains its unique serial number (ICCID). ICCIDs are stored in the SIM cards and are also printed on the SIM card during a personalisation process. Can have 18 to 20 digits


Solution

  • As per your question you want to get all numbers between 2 big numbers. you can use bignumber.js library https://www.npmjs.com/package/bignumber.js .it handles all operations related to big numbers.

    const BigNumber = require('bignumber.js');
    
    
    let arr = []; 
    let x = new BigNumber("100000000000000000");
    let y = "100000000000000029";
    while (x.isLessThanOrEqualTo(y)) {
        arr.push(x.toString());
        x = x.plus(1);
    }
    
    
    for (let elem of arr) {
        console.log(elem); //it contains all your elements
    }