Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

nodejs JavaScript heap out of memory

 //credits to "Andrey Ermakov" on stackoverflow
node --max-old-space-size=4096 yourFile.js
Comment

How to build a heap in JavaScript?

// Aim is to build a min-heap from an
// array of integers with support for following methods:
// buildHeap(array): to build the heap from array
// bubbleDown(currentIdx, endIdx, heap): to bubble an element down
// bubbleUp(currentIdx, heap): to bubble an element up
// peek(): returns min value without removing it
// remove(): removes min value
// insert(): inserts a new element into heap
class MinHeap {
  // Build heap using array of ints
  constructor(array) {
    this.heap = this.buildHeap(array);
  }
  // O(n) time | O(1) space
  buildHeap(array) {
    const firstParentIdx = Math.floor((array.length - 2) / 2);
    for (let currentIdx = firstParentIdx; currentIdx >= 0; currentIdx--) {
      this.bubbleDown(currentIdx, array.length - 1, array);
    }
    return array;
  }
  // O(log(n)) time | O(1) space
  bubbleDown(currentIdx, endIdx, heap) {
    let childOneIdx = currentIdx * 2 + 1;
    while (childOneIdx <= endIdx) {
      const childTwoIdx =
        currentIdx * 2 + 2 <= endIdx ? currentIdx * 2 + 2 : -1;
      let idxToSwap;
      if (childTwoIdx !== -1 && heap[childTwoIdx] < heap[childOneIdx]) {
        idxToSwap = childTwoIdx;
      } else {
        idxToSwap = childOneIdx;
      }
      if (heap[idxToSwap] < heap[currentIdx]) {
        this.swap(currentIdx, idxToSwap, heap);
        currentIdx = idxToSwap;
        childOneIdx = currentIdx * 2 + 1;
      } else {
        return;
      }
    }
  }
  // O(log(n)) time | O(1) space
  bubbleUp(currentIdx, heap) {
    let parentIdx = Math.floor((currentIdx - 1) / 2);
    while (currentIdx > 0 && heap[currentIdx] < heap[parentIdx]) {
      this.swap(currentIdx, parentIdx, heap);
      currentIdx = parentIdx;
      parentIdx = Math.floor((currentIdx - 1) / 2);
    }
  }
  // O(1) time | O(1) space
  peek() {
    return this.heap[0];
  }
  // O(log(n)) time | O(1) space
  remove() {
    this.swap(0, this.heap.length - 1, this.heap);
    const valueToRemove = this.heap.pop();
    this.bubbleDown(0, this.heap.length - 1, this.heap);
    return valueToRemove;
  }
  // O(log(n)) time | O(1) space
  insert(value) {
    this.heap.push(value);
    this.bubbleUp(this.heap.length - 1, this.heap);
  }
  swap(i, j, heap) {
    const temp = heap[i];
    heap[i] = heap[j];
    heap[j] = temp;
  }
}
Comment

nodejs heap usage

process.memoryUsage()
Comment

JavaScript heap out of memory error

export NODE_OPTIONS=--max_old_space_size=4096 #4GB
Comment

PREVIOUS NEXT
Code Example
Javascript :: laravel using react 
Javascript :: nodejs request 
Javascript :: character from character code js 
Javascript :: allow only numbers and special characters in textbox using javascript 
Javascript :: change source of image jquery 
Javascript :: javascript how to check if array is empty 
Javascript :: default value input date js 
Javascript :: import a script to my react componetn 
Javascript :: isnan javascript 
Javascript :: firestore set a document 
Javascript :: vue config devtools 
Javascript :: javascript sort numbers 
Javascript :: debounchow use input debounce in javascript vue.js 
Javascript :: correct json type 
Javascript :: firebase read data javascript 
Javascript :: controlled autocomplete material ui 
Javascript :: stop interval js 
Javascript :: npm numeral 
Javascript :: img src in react js 
Javascript :: loop an object in javascript 
Javascript :: float to currency js 
Javascript :: protected route in react js 
Javascript :: add an array to another array javascript 
Javascript :: npm react pagination 
Javascript :: smooth scroll mouse wheel javascript 
Javascript :: jquery hover event 
Javascript :: object get array of values 
Javascript :: get last word in string js 
Javascript :: jquery focus input end of text 
Javascript :: jquery div show 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =