Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9138781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:18:06+00:00 2026-06-17T09:18:06+00:00

I’m using typed arrays in a Javascript game to send data between the client

  • 0

I’m using typed arrays in a Javascript game to send data between the client (running in a browser) and the server (running node.js). I can send messages back and forth no problem but I’m interested in reusing my typed arrays rather than creating a new one each time I send a message.

Here’s my sendMessage code:

function sendMessage(socket, data) {

    // data is JSON
    var encoded = BISON.encode(data);
    if (socket.isBinary) {
        var len = encoded.length,
            bytes = new Uint32Array(len);

        for (var i = 0; i < len; ++i) {
            bytes[i] = encoded.charCodeAt(i);
        }
    }

    if (server && socket.isBinary) {
        socket.send(bytes, { binary: true, mask: false });
    } else {
        socket.send(encoded);
    }

}

and here’s my readMessage code:

function readMessage(data) {

    if (data instanceof ArrayBuffer) {
        var bytes = new Uint32Array(data);
        data = String.fromCharCode.apply(null, bytes);
    }
    var message = JSON.stringify(BISON.decode(data));
    return JSON.parse(message);
}

Each time I send a message I create a new Uint32Array, initializing it to the length of the message I intend to send. Rather than create a new array each time I’d like reuse this array for each message.

My current line of thought on this is to determine my maxMessageSize and initialize the array with this length. maxMessageSize would be the largest allowed message that one could send over the socket.

The Question(s)

Most messages are not likely to be exactly maxMessageSize in size. In these cases would I need to pad the length of the message with some value (null?). When reading the value on the other end of the socket how would I know when this padding began?

In writing all of this out another question has occurred to me: is this even worth the effort?

Libraries Used

  • ws for websockets in node.js
  • BISON for binary encoding of json.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:18:06+00:00Added an answer on June 17, 2026 at 9:18 am

    As far as I know there is no way to change the length of a Uint*Array once it has been constructed, you can only get sub arrays from them.

    Any padding would have to be done with 32-bit integers, if you tried to use null, ToInt(null) is 0 so you’d have a 0 in that entry. If you wanted to pad like this, you’d need to pass some length value (either first or last entry in array), which you’d need to look at on the received end to know when to trim the data and would mean maxMessageSize is one less.

    Using a subarray might be what you want though, construct your max-length array, fill it up, then use .subarray(0, len) to get the actual array you want. I’m not sure if this has any advantages over constructing a new one each time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am using jsonparser to parse data and images obtained from json response. When
I am reading a book about Javascript and jQuery and using one of the
I am using JSon response to parse title,date content and thumbnail images and place
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I have a small JavaScript validation script that validates inputs based on Regex. I
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.