Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

190
Vistas
Pass by value and memory consumption in JavaScript

Let's say I got this code:

function MinMax(list) {
  const min = Math.min(...list);
  const max = Math.max(...list);

  return {
    min,
    max,
    list,
  };
}

const massiveList = [
  1,
  // supposedly a gazillion
  // integer values
  10000000,
];

const minMax = MinMax(massiveList);

const minMaxList = minMax.list;

console.log('min              ', minMax.min);
console.log('max              ', minMax.max);
console.log('minMaxList length', minMaxList.length);

Here's my question: since arguments are passed by value in JavaScript, does that mean, that after that point in code, where minMax.list is assigned to minMaxList, three copies of the original massiveList exist in heap memory?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can try the below demo with reference value

const massiveList = [
  1,
  // supposedly a gazillion
  // integer values
  10000000,
];

const testReference = (list) => {
   console.log(list === massiveList) //true
}

testReference(massiveList)

In your case, list and massiveList are the same reference which means any modification on list will be applied on massiveList as well.

...list means you've cloned the original list to another list that will be allocated newly in the memory.

The below demo is to show reference values get changed for the new list, so that's why the result is false

const massiveList = [
  1,
  // supposedly a gazillion
  // integer values
  10000000,
];

const testReference = (list) => {
  const newList = [...list]
  console.log(list === newList) //false
}

testReference(massiveList)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda