0

I want to convert from ArrayBuffer (lets say 100MB) to Blob then back from Blob to ArrayBuffer without duplicating the data.
FileReader / XMLHttpRequest do duplicate the data, so is there an other way?

var b = new Blob([new Uint8Array(someArrayBuffer)]);

PS: I want to make cross-tab communication of large read only buffer without duplicating it, e.g. a local ArrayBuffer is 500MB I want to use it in multiple tabs without duplicating the data.

8
  • I don't think so, no. Commented Oct 28, 2017 at 8:48
  • Where is your code ? Commented Oct 28, 2017 at 8:48
  • You will always duplicate your data in a var or anything else.. FileReader() is robust, why you don't want to use it ? Commented Oct 28, 2017 at 8:49
  • @pirs i want a reference to it(not duplicate) but from blob object Commented Oct 28, 2017 at 8:59
  • developer.mozilla.org/fr/docs/Web/API/Body/arrayBuffer ? Maybe ? Commented Oct 28, 2017 at 9:02

1 Answer 1

1

There might be ways to convert a Blob to an ArrayBuffer without a FileReader or XHR, but they would all duplicate the data anyway.

But it sounds that what you need is not to convert from a Blob to an ArrayBuffer, but rather to share your ArrayBuffer or Blob across tabs.

For this, you've got multiple ways:

  • Store the ArrayBuffer/Blob on the main page, and access it from the children pages directly, like you could access any other global variables:

    // in parent
    window.myArrayBuffer = buffer;
    
    // in child
    var buf = window.opener.myArrayBuffer;
    doSomethingWithBuffer(buf);
    

    Plnkr.

  • If you need to access the ArrayBuffer only in one tab at a time, then you can transfer its buffer to the other pages, with the postMessage API.

    // in parent
    child.postMessage(myArrayBuffer, 'origin', [myArrayBuffer]);
    // here myArrayBuffer's buffer is *transferred* to child,
    // it can't be accessed by parent anymore
    

    // in child
    window.onmessage = e => {
      buf = e.data;
      ...
    

    Plnkr.

  • If you only need to display a Blob on different but from same-origin windows, then use a blobURI. It will only create a pointer to the data in memory, and lock this data from Garbage Collection until your docs die, but will not duplicate the data.

  • If you don't need to support IE nor Safari, you may even consider SharedWorkers on which you will first transfer your ArrayBuffer, and let all your tabs ask it to perform calculations, based on the same data.

Sign up to request clarification or add additional context in comments.

4 Comments

the sharedworkers idea is nice if I use bloburi from different windows I will still need to duplicate the buffer from other window when I want to access the ArrayBuffer of Blob ,Am I right ? because Blob doesn't have a field as buffer
@user818117 blobURIs are to display/dowload-on-user-disk the data as a file. Don't use it to share an ArrayBuffer, it makes no sense. Did you missed the first two bullets? To share any variable between same origin frames simply access it. For cross-origin ones, transfer it with postMessage.
how can I transfer pointer or post message if I want to exchange the buffer pointer between multiple tabs but same origin, not parent or child
@user818117 from web script, you simply can't, that would be a huge security threat to know what are the other open tabs. From chrome: scripts (extension), then you probably can. But it is way easier for you to simply open these pages from your own script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.