Learn Node JS
Node js buffer
- Why is the buffer class used in Node.js?In Node.js, the buffer class stores the raw data, in a manner similar to that of an array of integers. However, it communicates to a raw memory dump, allocated outside the V8 heap. The Buffer class is a global class and can be accessed in an application without having to import the buffer module. It is majorly used as pure JavaScript code is not attuned with binary data.
- List some advantages of using Node.jsIt is fast because it is created on the Chrome JavaScript engine, which makes its library faster in executing code. It is Asynchronous because it never waits for an API to give back data. It is scalable because of its event mechanism, which enables the server to respond in a non-blocking way. It is an open-source, offering an incredible open-source community, which has created some fantastic modules which added more power to Node.js apps. Node.js never buffers data because the output is in chunks.
- In Node.js, what is the difference between createReadStream and readFileNode.js offers two ways to read and operate files that are using readFile and CreateStream. readFile() is a completely buffered process that responds only when the entire file is pushed in the buffer and read. It is a memory-intensive process, which gets very slow for larger files. Whereas in the case of createReadStream is partially buffered, which takes the complete process as an event series. The file is divided into big chunks, which are then processed and sent back as a response one at a time. Once completed, they are then removed from the buffer, making it more efficient for processing larger files.
- Explain readFile and createReadStream in Node.js.Both readFile and createReadStream are ways to read and execute files provided by the Node.js. The readFile process is fully buffered which returns response(s) only if the complete file is entered into the buffer and can be read. This process is highly memory intensive and can become slow in case the file size is large. The createReadStream process is partially buffered, treating the entire process as a series of events. In this process, the whole files are split into chunks that are processed and then sent as a response individually in succession. Unlike readFile, createReadStream is effective when it comes to reading and processing large files.
- What are node.js buffers?In general, buffers is a temporary memory that is mainly used by stream to hold on to some data until consumed. Buffers are introduced with additional use cases than JavaScript’s Unit8Array and are mainly used to represent a fixed-length sequence of bytes. This also supports legacy encodings like ASCII, utf-8, etc. It is a fixed(non-resizable) allocated memory outside the v8.
- How to measure the duration of async operations?Performance API provides us with tools to figure out the necessary performance metrics. A simple example would be using async_hooks and perf_hooks 'use strict'; const async_hooks = require('async_hooks'); const { performance, PerformanceObserver } = require('perf_hooks'); const set = new Set(); const hook = async_hooks.createHook({ init(id, type) { if (type === 'Timeout') { performance.mark(`Timeout-${id}-Init`); set.add(id); } }, destroy(id) { if (set.has(id)) { set.delete(id); performance.mark(`Timeout-${id}-Destroy`); performance.measure(`Timeout-${id}`, `Timeout-${id}-Init`, `Timeout-${id}-Destroy`); } } }); hook.enable(); const obs = new PerformanceObserver((list, observer) => { console.log(list.getEntries()[0]); performance.clearMarks(); observer.disconnect(); }); obs.observe({ entryTypes: ['measure'], buffered: true }); setTimeout(() => {}, 1000); This would give us the exact time it took to execute the callback.
4iv>