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

245
Vistas
Is passing Int32Array instead of SharedArrayBuffer supposed to work?

I have some code that creates a shared array buffer and passes the Int32Array and NOT the SharedArrayBuffer to the worker:

let worker = new Worker(...);
worker.postMessage({
  position: {
    x: createArray(),
    y: createArray()
  },
  velocity: {
    x: createArray(),
    y: createArray()
  }
});

function createArray(size = 1024) {
  let buffer = new SharedArrayBuffer(size);
  return new Int32Array(buffer);
}

In the worker I'm updating the resulting integers in a loop across the array.

let components = e.data;

let velocityX = Atomics.load(components.velocity.x, shipEid);
let velocityY = Atomics.load(components.velocity.y, shipEid);
Atomics.add(components.position.x, shipEid, velocityX);
Atomics.add(components.position.y, shipEid, velocityY);

Now, my question is, is it valid to pass the Int32Array to the worker instead of the SharedArrayBuffer? I honestly didn't even realize I was doing this until much later, and now that I realize it I'm wondering why it works. I thought you had to pass the SharedArrayBuffer to the worker in order to share the memory between the worker and the main process. Is this actually part of the spec to work this way, or is this something Chrome specific that will break for other browsers and possibly break in Chrome in the future?

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

0

Yes, this is valid. Notice that you're not actually passing the Int32Array either: you're passing an object which has properties that are objects which have properties that are typed arrays. This works because the argument to postMessage is actually structurally cloned, and when the cloning algorithm reaches the SharedArrayBuffer that is part of your structure then it is cloned into a new buffer object that shares the underlying memory.

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