Skip to content

Reading binary into an existing buffer #111

Closed
@domenic

Description

@domenic

@dherman brought this up originally, and then it came up in a conversation with @willchan and @slightlyoff.

The idea would be something like:

var ab = rs.read();

// OR
var bytesWritten = rs.readInto(ab, offset);

There's lots of bikeshedding that could be done here (overload instead of separate method? What does the prior art in other languages and libraries do? How to handle overflows? etc.). But the argument is quite sound in principle: it would allow you to allocate a single contiguous array buffer, then fill it with multiple reads from the stream.

The alternative now would be concatenating array buffers, which (a) probably involves a copy in all existing browsers; (b) even if it were optimized (in a similar manner to string concatenation), would cause memory fragmentation/cache locality problems compared to the single contiguous buffer.

This would be for binary (and string?) streams only. It may be a good use case for a BinaryReadableStream subclass.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions