Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
Can I make JavaScript use a fixed array over a dynamic array when making a Pascal's triangle generator?

More specifically, am I actually using arrays here. I never declared the size of the array, I just start adding numbers to the chosen index.

To me, this is suspicious, and makes me think that JavaScript might be using a hash map instead of an array, which would involve more computation.

Can I make JavaScript use a fixed array over a dynamic array over a hash?

Is there a way to verify the underlying data structure without digging through the code base?

var generate = function(n) {
  const matrix = [[1], [1, 1]];

  // base case : n = 1
  if(n === 1) {
    return [[1]];
  }

  // base case : n = 2
  if(n === 2) {
    return matrix;
  }

  // first iterative case : n = 3
  // be careful of off by one error here
  // n is number of rows (3), but i is 0 based (2)
  for(let i = 2; i < n; i++) {

    // add empty row
    matrix[i] = [];

    for(let j = 0; j <= i; j++) {
      // if first or last element set to 1
      if( j === 0 || j === i ) {
        matrix[i][j] = 1;
      } else {
        matrix[i][j] = matrix[i-1][j-1] + matrix[i-1][j]
      }
    }
  }
  return matrix;

};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

makes me think that JavaScript might be using a hash map instead of an array, which would involve more computation.

Without looking at the actual code, this is true. In JavaScript an array is just a plain object (i.e. dictionary) with additional features. The indexes are properties that happen to represent non-negative integer (in a certain range). Although the engine might store the array as a consecutive array (as you would expect in C-style languages), the engine is free to really use a hash table like it would for any other object. It may even decide at run time to switch from one internal representation to another (transparent to the code).

Can I make JavaScript use a fixed array over a dynamic array over a hash?

Yes, there are typed arrays, like Int16Array

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!