0

Javascript supports reading and writing to binary arrays with the typed array interface.

Essentially it creates a memory buffer of binary data, and then reads that binary data through a "view". This view tells the browser how many bits of data should be read at a time and makes an array out of it..

For example the bits 0000000100000001 would look like this through different views:

// 8 bit (char) array
charArray[0] = 1;
charArray[1] = 1;

// 16 bit (short int) array
intArray[0] = 257;

For a project I am working on I need to store only 1 bit of data at a time, and file-size is a concern. The smallest built in array size Javascript has is a char array, and using a char array to just save either 1 or 0 would be a waste of space because it has to store all the leading 0's.

My Question: What would be the fastest way in terms of performance to read binary data 1 bit at a time in Javascript?

I know javascript supports bitwise operators, but I can't find anything about their performance. The only other thing I can think of would be to use a char array, and write custom logic that would subtract the proper number to read only one bit at a time.

Any help would be much appreciated.

3
  • What do you want to do with the bits? Performance wise, you should care about using bitwise operations roughly as much as you care about +. Commented Jun 3, 2018 at 6:21
  • @ASDFGerte The bits are used to create the structure of a custom tree. It's basically a binary tree mixed with a quad tree depending on the level of branching. Do you have any idea how much of a performance hit I would run into? I guess it would be 1 addition operation per memory lookup right? Commented Jun 3, 2018 at 6:23
  • The logic for what to do with that bit is likely to take a lot more than actually retrieving the bit. That's why imho it's much more interesting whether the steps afterwards can be combined and possibly optimized than how to read the bit. I don't know specifics on how interpretation/jit handles these operations, but bitwise operations are easier than addition from a processor point of view. Due to fixed clock ticks etc it's likely they will take approximately the same time though, unless some extra optimization i don't know about makes bitwise operations faster again. Commented Jun 3, 2018 at 6:28

1 Answer 1

0

As far as I know, bitwise operations in almost all languages are extremely fast.

If you read and store 8 bits at a time (which is usually the smallest size languages provide for) you should have no trouble meeting your time requirements.

Extracting the ith bit in an 8 bit value is fast if you implement it as a rightshift and a bitwise and (or a leftshift and a rightshift).

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

Comments

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.