Quick Contact


    BUFFERS IN NODE JS

    Buffers help developers to deal with binary data. In ecosystem that traditionally only deals with strings rather than binaries. Buffers deeply link with streams when the stream processor receives the data faster than it can digest, it puts the data in buffer, Stream in node JS simply means a sequence of data being moved from one point to other point over time. The whole concept is you have a huge amount of data to process but you don’t need to wait for all the data to be available before you start processing it.

    You can think of buffer like an array of integers which each represent a byte of data.

    Array- [79,59,49,39,30,19]

    Let’s take example with code-:

    Take a variable constant “buff” is equal to and call a buffer class here and from this buffer call a method from as-:

    Constbuf = buffer.from (“hey”)

    So this is kind of string and then after it is as-:

    Console.log (buf)

    When you execute these two commands-:

    Result is -:
    buffers-in-nodejs

    Buffer is like an array so you can access it using square bracket as-:

    Console.log (buf[0])

    Now if you execute file by adding this line.

    Result is as-:
    buffers-in-nodejs

    So as you notice result is 72. As you have three numbers represent this string and this 72 represent this first edge character these numbers and unique code that identifies the character in the buffer position so here’s the buffer.

    So, let’s duplicate these statements and write 1 and 2 at the place of 0 as in code. And execute

    buffers-in-nodejs

    As you see the result is 101 and 121. So here from “hey”, ‘h’ has unit code 72, ‘e’ has unit code 101 and ‘y’ has unit code 121. Now print this buffer using “tostring” and execute this file.

    buffers-in-nodejs

    So as you see above result of “tostring” method, so this buffer is going to print this message using this “tostring” method. As buffer is going to take the data as binaries, now here buffer class is called and call the ‘from’ method of buffer class to create this buffer, now if you want you can write to a buffer a whole string of data by using a “write method”. To get rid of this method, call here the “allocate” method (to allocate a memory for the buffer) as-:

    Constbuf = buffer.alloc(4)

    And specify 4 using “buffer.write” method as-:

    Buf.write(“hey!”)

    It means you allocate the size of this buffer- 4, so specify here four characters inside this “write method”.

    Now save the change and execute-:
    buffers-in-nodejs

    So here is the same result, so using this technique, you can also write your own buffer. As you notice you can access the buffer using array index. You are not limited to only access the buffer index. Just like you can access a buffer with array syntax. You can also set the content of the buffer in the same way.

    Now let’s add something more. Let’s say-:

    Buf and first index 111 which is Equal to “o” this is unique code of ‘o’as-:

    Buf[1] = 111 // o and write console after

    Now print this buffer as-:

    buffers-in-nodejs

    So as you can notice, having ‘o’ at the first index of this buffer, so it’s all how to work with buffer.

    Copyright 1999- Ducat Creative, All rights reserved.