nodejs 14.0.0中FixedQueue的作用是什么

发布时间:2021-07-21 09:04:38 作者:Leah
来源:亿速云 阅读:217

nodejs 14.0.0中FixedQueue的作用是什么,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

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的作用是什么

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

推荐阅读:
  1. nodejs有什么作用
  2. Nodejs中cluster模块的作用是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

node.js

上一篇:微信如何通过html5页面直接打开本地app

下一篇:NodeJS中怎么将网络图片转换为base64

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》