Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

106
Visualizações
How to verify JavaScript memory consumption by a piece of code

both javascript code accomplish the same task merge the second array into the first one, i have read various blogs etc claming Option 2 is more memory efficient,

Is there a way or a tool to verify and see it for myself the memory usage in the Option 2 is lower ?

Option 1

//consume a lot of memory
var array1 = [1, 2, 3]; 
var array2 = [4, 5, 6]; 
console.log(array1.concat(array2));

Option 2

//reduce the memory usage
var array1 = [1, 2, 3]; 
var array2 = [4, 5, 6]; 
console.log(array1.push.apply(array1, array2));

i tried this approach https://www.valentinog.com/blog/node-usage/ node js code, not much helpful also tried https://github.com/paulirish/memory-stats.js dosent seems to work

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your premise is wrong. Both scripts do not accomplish the same task.

Your first option allocates a new result array with 6 elements, and leaves both array1 and array2 untouched. (See the documentation for Array.prototype.concat().)

var array1 = [1, 2, 3]; 
var array2 = [4, 5, 6]; 
console.log(array1.concat(array2)); // [1, 2, 3, 4, 5, 6]
console.log(array1);  // [1, 2, 3]
console.log(array2);  // [4, 5, 6]

Your second option merges the contents of array2 into array1. (See the documentation for Array.prototype.push().)

var array1 = [1, 2, 3]; 
var array2 = [4, 5, 6]; 
console.log(array1.push.apply(array1, array2)); // 6
console.log(array1);  // [1, 2, 3, 4, 5, 6]
console.log(array2);  // [4, 5, 6]

So the second option only requires a total of 9 array elements, while the first option requires 12. On a language level, option 2 is 25% more memory-efficient. How much memory is actually used by the JavaScript engine you're using is an implementation detail. In addition, contrived examples like these are typically not good candidates for reliable performance testing as the engine's JavaScript interpreter might apply various optimizations.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda