Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the role of FixedQueue in nodejs 14.0.0

Shulou Source: shulou.com Published: 2022-06-02 06:31:23 10月03日 Update

What is the role of FixedQueue in nodejs 14.0.0? for this question, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

FixedQueue is used to implement nextTick. There's not much code.

'use strict'

Const {Array,} = primordials

/ / Currently optimal queue size, tested on V8 6.0-6.6. Must be power of two.const kSize = 2048th Const kMask = kSize-1

Const FixedCircularBuffer = class FixedCircularBuffer {constructor () {this.bottom = 0; this.top = 0; this.list = new Array (kSize); this.next = null;}

IsEmpty () {return this.top = this.bottom;} / / to determine the loop isFull () {return ((this.top + 1) & kMask) = this.bottom;}

Push (data) {this.list [this.top] = data; this.top = (this.top + 1) & kMask;} / / remove an element, update the location shift () {const nextItem = this.list [this.bottom]; / / there are no elements, and there is no need to update the location if (nextItem = = undefined) return null; this.list [this.bottom] = undefined This.bottom = (this.bottom + 1) & kMask; return nextItem;}}

Module.exports = class FixedQueue {constructor () {this.head = this.tail = new FixedCircularBuffer ();}

IsEmpty () {return this.head.isEmpty ();}

If push (data) {/ / is full, apply for a new one. Head points to the new one, and tail points to the beginning, that is, the oldest if (this.head.isFull ()) {/ / Head is full: Creates a new queue, sets the old queue's `next` to it, / / and sets it as the new main queue. This.head = this.head.next = new FixedCircularBuffer ();} this.head.push (data);}

Shift () {const tail = this.tail; const next = tail.shift (); / / after consuming a FixedCircularBuffer, the next if (tail.isEmpty () & & tail.next! = = null) {/ / If there is another queue, it forms the new tail. This.tail = tail.next;} return next;}}

The answer to the question about what is the role of FixedQueue in nodejs 14.0.0 is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Tags: Questions roles locations elements directions more help updates answers easy easy code content loops guys partners methods knowledge articles industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Xiaomi MySQL macOS OPPO Reno