As I known.
The shift command is come from the binary bit shift [1]. for Example.
001100
0 < 011000 // when you shift left
|
Yay!
I think it is quite simple it is just like you push it from behind. So this makes sense for me.
The unshift is the opposite way of shift.
001100
1 > 001100 // they called it unshift
1001100
|
Yay!
So that's it, Hope this helps!
[1] http://en.wikipedia.org/wiki/Bitwise_operation#Bit_shifts
Answer from Chokchai on Stack OverflowVideos
As I known.
The shift command is come from the binary bit shift [1]. for Example.
001100
0 < 011000 // when you shift left
|
Yay!
I think it is quite simple it is just like you push it from behind. So this makes sense for me.
The unshift is the opposite way of shift.
001100
1 > 001100 // they called it unshift
1001100
|
Yay!
So that's it, Hope this helps!
[1] http://en.wikipedia.org/wiki/Bitwise_operation#Bit_shifts
a.push(e) pushes e onto the end of a.
e = a.pop() pops the last element from a, to e.
a.unshift(e) enqueues e to the start of a.
e = a.shift() gets the first element from a to e.
Use push and pop for stacks.
Use unshift and pop for queues. (Or push and shift)
I remember the difference between shift (destructive) and unshift (constructive) simply by remembering that I use un-shift for en-queueing, and shift is the opposite to unshift.
EDIT: This is just venting, not confusion on how the word is technically used by JS :D
OK, this is why I keep starting and stopping learning JS- reserved words with extremely unrelated English meanings. "Unshift" means a previous shift occurred. That's the only meaning for it in English. But JS decided to use it as a prepend array method. Argh!!
That is all...
push() is faster.
js>function foo() {a=[]; start = new Date; for (var i=0;i<100000;i++) a.unshift(1); return((new Date)-start)}
js>foo()
2190
js>function bar() {a=[]; start = new Date; for (var i=0;i<100000;i++) a.push(1); return((new Date)-start)}
js>bar()
10
function foo() {a=[]; start = new Date; for (var i=0;i<100000;i++) a.unshift(1); return((new Date)-start)}
console.log(foo())
function bar() {a=[]; start = new Date; for (var i=0;i<100000;i++) a.push(1); return((new Date)-start)}
console.log(bar());
Update
The above does not take into consideration the order of the arrays. If you want to compare them properly, you must reverse the pushed array. However, push then reverse is still faster by ~10ms for me on chrome with this snippet:
var a=[];
var start = new Date;
for (var i=0;i<100000;i++) {
a.unshift(1);
}
var end = (new Date)-start;
console.log(`Unshift time: ${end}`);
var a=[];
var start = new Date;
for (var i=0;i<100000;i++) {
a.push(1);
}
a.reverse();
var end = (new Date)-start;
console.log(`Push and reverse time: ${end}`);
The JavaScript language spec does not mandate the time complexity of these functions, as far as I know.
It is certainly possible to implement an array-like data structure (O(1) random access) with O(1) push and unshift operations. The C++ std::deque is an example. A Javascript implementation that used C++ deques to represent Javascript arrays internally would therefore have O(1) push and unshift operations.
But if you need to guarantee such time bounds, you will have to roll your own, like this:
http://code.stephenmorley.org/javascript/queues/