Skip to content

Commit b6d0845

Browse files
committed
new stack system
1 parent 93bfaff commit b6d0845

File tree

4 files changed

+180
-17
lines changed

4 files changed

+180
-17
lines changed

benchmark/stacks.js

Lines changed: 164 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,196 @@
11
let { stacks } = require('./../index');
22
let ptime = require('quick-ptime');
3+
const Table = require('cli-table');
34

45

56
// setup stacks
67
let linkedStack = new stacks.LinkedStack();
78
let uArrayStack = new stacks.UArrayStack();
8-
let bArrayStack = new stacks.BArrayStack(100000);
9+
let bArrayStack = new stacks.BArrayStack(1100001);
10+
11+
/**
12+
* PUSH STUFF
13+
*/
914

1015
// push 100,000 items onto the stack
1116
ptime.setTime('stack100000');
1217
for(let i = 0; i < 100000; i++) {
1318
linkedStack.push('value');
1419
}
15-
console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);
20+
let stackTime = ptime.elapsedTime('stack100000');
21+
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);
22+
23+
// push 1,000,000 items onto the stack
24+
ptime.setTime('stack1000000');
25+
for(let i = 0; i < 1000000; i++) {
26+
linkedStack.push('value');
27+
}
28+
let mStackTime = ptime.elapsedTime('stack1000000');
29+
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);
30+
31+
// push 1 item onto the stack
32+
ptime.setTime('stack1');
33+
for(let i = 0; i < 1; i++) {
34+
linkedStack.push('value');
35+
}
36+
let sStackTime = ptime.elapsedTime('stack1');
37+
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);
38+
1639

17-
ptime.setTime('uarraystack100000');
40+
41+
42+
43+
// push 100,000 items onto the stack
44+
ptime.setTime('ustack100000');
1845
for(let i = 0; i < 100000; i++) {
1946
uArrayStack.push('value');
2047
}
21-
console.log("Push 100,000 Unbounded Array Stack timing:", ptime.elapsedTime('uarraystack100000').formatted);
48+
let ustackTime = ptime.elapsedTime('ustack100000');
49+
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);
50+
51+
// push 1,000,000 items onto the stack
52+
ptime.setTime('ustack1000000');
53+
for(let i = 0; i < 1000000; i++) {
54+
uArrayStack.push('value');
55+
}
56+
let umStackTime = ptime.elapsedTime('ustack1000000');
57+
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);
2258

23-
ptime.setTime('barraystack100000');
59+
// push 1 item onto the stack
60+
ptime.setTime('ustack1');
61+
for(let i = 0; i < 1; i++) {
62+
uArrayStack.push('value');
63+
}
64+
let usStackTime = ptime.elapsedTime('ustack1');
65+
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);
66+
67+
68+
69+
70+
// push 100,000 items onto the stack
71+
ptime.setTime('bstack100000');
2472
for(let i = 0; i < 100000; i++) {
2573
bArrayStack.push('value');
2674
}
27-
console.log("Push 100,000 Bounded Array Stack timing:", ptime.elapsedTime('barraystack100000').formatted);
75+
let bstackTime = ptime.elapsedTime('bstack100000');
76+
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);
77+
78+
// push 1,000,000 items onto the stack
79+
ptime.setTime('bstack1000000');
80+
for(let i = 0; i < 1000000; i++) {
81+
bArrayStack.push('value');
82+
}
83+
let bmStackTime = ptime.elapsedTime('bstack1000000');
84+
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);
85+
86+
// push 1 item onto the stack
87+
ptime.setTime('bstack1');
88+
for(let i = 0; i < 1; i++) {
89+
bArrayStack.push('value');
90+
}
91+
let bsStackTime = ptime.elapsedTime('bstack1');
92+
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);
93+
2894

2995

30-
// pop 100,000 items off the stack
96+
97+
/**
98+
* POP STUFF
99+
*/
100+
101+
// push 100,000 items onto the stack
31102
ptime.setTime('pstack100000');
32103
for(let i = 0; i < 100000; i++) {
33-
linkedStack.pop()
104+
linkedStack.pop('value');
105+
}
106+
let pstackTime = ptime.elapsedTime('pstack100000');
107+
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);
108+
109+
// push 1,000,000 items onto the stack
110+
ptime.setTime('pstack1000000');
111+
for(let i = 0; i < 1000000; i++) {
112+
linkedStack.pop('value');
113+
}
114+
let pmStackTime = ptime.elapsedTime('pstack1000000');
115+
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);
116+
117+
// push 1 item onto the stack
118+
ptime.setTime('pstack1');
119+
for(let i = 0; i < 1; i++) {
120+
linkedStack.pop('value');
34121
}
35-
console.log("Pop 100,000 Stack timing:", ptime.elapsedTime('pstack100000').formatted);
122+
let psStackTime = ptime.elapsedTime('pstack1');
123+
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);
124+
36125

37-
ptime.setTime('puarraystack100000');
126+
127+
// push 100,000 items onto the stack
128+
ptime.setTime('upstack100000');
38129
for(let i = 0; i < 100000; i++) {
39-
uArrayStack.pop()
130+
uArrayStack.pop('value');
131+
}
132+
let upstackTime = ptime.elapsedTime('upstack100000');
133+
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);
134+
135+
// push 1,000,000 items onto the stack
136+
ptime.setTime('upstack1000000');
137+
for(let i = 0; i < 1000000; i++) {
138+
uArrayStack.pop('value');
40139
}
41-
console.log("Pop 100,000 Unbounded Array Stack timing:", ptime.elapsedTime('puarraystack100000').formatted);
140+
let upmStackTime = ptime.elapsedTime('upstack1000000');
141+
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);
142+
143+
// push 1 item onto the stack
144+
ptime.setTime('upstack1');
145+
for(let i = 0; i < 1; i++) {
146+
uArrayStack.pop('value');
147+
}
148+
let upsStackTime = ptime.elapsedTime('upstack1');
149+
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);
150+
42151

43-
ptime.setTime('pbarraystack100000');
152+
153+
154+
155+
// push 100,000 items onto the stack
156+
ptime.setTime('bpstack100000');
44157
for(let i = 0; i < 100000; i++) {
45-
bArrayStack.pop()
158+
bArrayStack.pop('value');
159+
}
160+
let bpstackTime = ptime.elapsedTime('bpstack100000');
161+
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);
162+
163+
// push 1,000,000 items onto the stack
164+
ptime.setTime('bpstack1000000');
165+
for(let i = 0; i < 1000000; i++) {
166+
bArrayStack.pop('value');
167+
}
168+
let bpmStackTime = ptime.elapsedTime('bpstack1000000');
169+
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);
170+
171+
// push 1 item onto the stack
172+
ptime.setTime('bpstack1');
173+
for(let i = 0; i < 1; i++) {
174+
bArrayStack.pop('value');
46175
}
47-
console.log("Pop 100,000 Bounded Array Stack timing:", ptime.elapsedTime('pbarraystack100000').formatted);
176+
let bpsStackTime = ptime.elapsedTime('bpstack1');
177+
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);
178+
179+
180+
/// Output results
181+
182+
const table = new Table({
183+
head: ['Stack Operation', '100,000 items', '1,000,000 items', '1 item']
184+
});
185+
186+
187+
table.push(
188+
['Linked List Stack Push', stackTime.formatted, mStackTime.formatted, sStackTime.formatted],
189+
['Unbounded Array Stack Push', ustackTime.formatted, umStackTime.formatted, usStackTime.formatted],
190+
['Bounded Array Stack Push', bstackTime.formatted, bmStackTime.formatted, bsStackTime.formatted],
191+
['Linked List Stack Pop', pstackTime.formatted, pmStackTime.formatted, psStackTime.formatted],
192+
['Unbounded Array Stack Pop', upstackTime.formatted, upmStackTime.formatted, upsStackTime.formatted],
193+
['Bounded Array Stack Pop', bpstackTime.formatted, bpmStackTime.formatted, bpsStackTime.formatted],
194+
);
195+
196+
console.log(table.toString());

lib/stacks.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class LinkedStack {
22
constructor () {
3-
this.topNode = {};
3+
this.topNode = null;
44
this.length = 0;
55
}
66

@@ -15,7 +15,7 @@ class LinkedStack {
1515
}
1616

1717
pop() {
18-
if(this.length === 0) {
18+
if(this.isEmpty()) {
1919
return undefined;
2020
}
2121

@@ -89,6 +89,7 @@ class BArrayStack {
8989
if(!this.isFull()) {
9090
this.topIndex++;
9191
this.stack[this.topIndex] = item;
92+
return this.height();
9293
} else {
9394
throw new Error('Stack too deep');
9495
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"mocha": "^5.2.0"
1818
},
1919
"dependencies": {
20+
"cli-table": "^0.3.1",
2021
"quick-ptime": "^1.0.0"
2122
}
2223
}

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ [email protected]:
2020
resolved "https://registry.yarnpkg.com/browser-stdout/-/browser-stdout-1.3.1.tgz#baa559ee14ced73452229bad7326467c61fabd60"
2121
integrity sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==
2222

23+
cli-table@^0.3.1:
24+
version "0.3.1"
25+
resolved "https://registry.yarnpkg.com/cli-table/-/cli-table-0.3.1.tgz#f53b05266a8b1a0b934b3d0821e6e2dc5914ae23"
26+
integrity sha1-9TsFJmqLGguTSz0IIebi3FkUriM=
27+
dependencies:
28+
colors "1.0.3"
29+
30+
31+
version "1.0.3"
32+
resolved "https://registry.yarnpkg.com/colors/-/colors-1.0.3.tgz#0433f44d809680fdeb60ed260f1b0c262e82a40b"
33+
integrity sha1-BDP0TYCWgP3rYO0mDxsMJi6CpAs=
34+
2335
2436
version "2.15.1"
2537
resolved "https://registry.yarnpkg.com/commander/-/commander-2.15.1.tgz#df46e867d0fc2aec66a34662b406a9ccafff5b0f"

0 commit comments

Comments
 (0)