nodejs 14.0.0中FixedQueue的作用是什么,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都做网站、成都网站设计、魏都网络推广、微信小程序开发、魏都网络营销、魏都企业策划、魏都品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供魏都建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com
FixedQueue是用来实现nextTick的。代码不多。
'use strict';const {Array,} = primordials;// Currently optimal queue size, tested on V8 6.0 - 6.6. Must be power of two.const kSize = 2048;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;}// 要判断回环isFull() {return ((this.top + 1) & kMask) === this.bottom;}push(data) {this.list[this.top] = data;this.top = (this.top + 1) & kMask;}// 移除一个元素,更新位置shift() {const nextItem = this.list[this.bottom];// 没有元素了,不需要更新位置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();}push(data) {// 满了则申请一个新的,head指向新的,tail指向最开始的那个,即最旧的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();// 消费完一个FixedCircularBuffer了,下一个if (tail.isEmpty() && tail.next !== null) {// If there is another queue, it forms the new tail.this.tail = tail.next;}return next;}};

关于nodejs 14.0.0中FixedQueue的作用是什么问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。