[英]Best way to implement a queue in javascript?
Hi I want to use a queue in javascript. 嗨我想在javascript中使用队列。 So I guess I can do 1 of 3 things:
所以我想我可以做三件事之一:
javascript push, shift javascript推,转移
array.push(), array[0], array.splice(0,1) etc. array.push(),array [0],array.splice(0,1)等。
Queue.js at http://code.stephenmorley.org/javascript/queues/#download Queue.js在http://code.stephenmorley.org/javascript/queues/#download
So I was reading the queue.js and was confused about the benchmarks because I don't really know what the numbers mean. 所以我正在阅读queue.js并对基准测试感到困惑,因为我真的不知道这些数字是什么意思。 Also, I'm guessing there are better ways of doing a queue than the 3 I mentioned.
另外,我猜测有一个比我提到的3更好的队列排队方式。
So what's the best way of implementing a queue in javascript and why? 那么在javascript中实现队列的最佳方法是什么?为什么? Also if anyone could explain the advantages and disadvantages among the 3 ways I described, that would be very helpful.
如果有人能够解释我描述的3种方式中的优点和缺点,那将非常有帮助。 Thanks !
谢谢 !
Here is a basic Queue definition, which works just perfectly for me. 这是一个基本的队列定义,对我来说非常合适。
queue: function() {
var items;
this.enqueue = function(item) {
if (typeof(items) === 'undefined') {
items = [];
}
items.push(item);
}
this.dequeue = function() {
return items.shift();
}
this.peek = function(){
return items[0];
}
}
Queue.js is using something like your second proposition but it's less efficient because it requires unneeded tests, native methods are better optimized. Queue.js正在使用类似你的第二个命题,但效率较低,因为它需要不需要的测试,本机方法更好地优化。 I would definitively use Sacket's solution.
我肯定会使用Sacket的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.