PRZYKŁADOWY KOD -ODWROTNA NOTACJA POLSKA
for (let i = 0; i < 10000; i++) {
let stack = [];
let str = '12 2 3 4 * 10 5 / + * +';
str = str.split(" ");
let output = [];
let currentOp = [];
let num = 0;
let string;
for (let i in str) {
num = parseInt(str[i]);
if (isNaN(num)) {
string = num.toString();
stack.push(str[i]);
if (str.length > 0) {
if (isNaN(str[i])) {
output.push(stack.pop());
currentOp = [];
for (let i = 0; i < 3; i++) {
currentOp.push(output.pop());
this.popped = currentOp.reverse();
let x = currentOp[0];
let y = currentOp[2];
this.result = 0;
if (currentOp[1] === "*") { result = x * y }
else if (currentOp[1] === "/") { result = x / y }
else if (currentOp[1] === "-") { result = x - y }
else if (currentOp[1] === "+") { result = x + y }
else if (currentOp[1] === "^") { result = Math.pow(x, y) }
}
output.push(result);
}
}
} else {
output.push(num);
}
}
}
9.
PRZYKŁADOWY KOD -ODWROTNA NOTACJA POLSKA
for (let i = 0; i < 10000; i++) {
let stack = [];
let str = '12 2 3 4 * 10 5 / + * +';
str = str.split(" ");
let output = [];
let currentOp = [];
let num = 0;
let string;
for (let i in str) {
num = parseInt(str[i]);
if (isNaN(num)) {
string = num.toString();
stack.push(str[i]);
if (str.length > 0) {
if (isNaN(str[i])) {
output.push(stack.pop());
currentOp = [];
for (let i = 0; i < 3; i++) {
currentOp.push(output.pop());
this.popped = currentOp.reverse();
let x = currentOp[0];
let y = currentOp[2];
this.result = 0;
if (currentOp[1] === "*") { result = x * y }
else if (currentOp[1] === "/") { result = x / y }
else if (currentOp[1] === "-") { result = x - y }
else if (currentOp[1] === "+") { result = x + y }
else if (currentOp[1] === "^") { result = Math.pow(x, y) }
}
output.push(result);
}
}
} else {
output.push(num);
}
}
}
(2+3)*5
2 3 + 5 *
w odwrotnej
notacji polskiej
10.
PRZYKŁADOWY KOD -ODWROTNA NOTACJA POLSKA
for (let i = 0; i < 10000; i++) {
let stack = [];
let str = '12 2 3 4 * 10 5 / + * +';
str = str.split(" ");
let output = [];
let currentOp = [];
let num = 0;
let string;
for (let i in str) {
num = parseInt(str[i]);
if (isNaN(num)) {
string = num.toString();
stack.push(str[i]);
if (str.length > 0) {
if (isNaN(str[i])) {
output.push(stack.pop());
currentOp = [];
for (let i = 0; i < 3; i++) {
currentOp.push(output.pop());
this.popped = currentOp.reverse();
let x = currentOp[0];
let y = currentOp[2];
this.result = 0;
if (currentOp[1] === "*") { result = x * y }
else if (currentOp[1] === "/") { result = x / y }
else if (currentOp[1] === "-") { result = x - y }
else if (currentOp[1] === "+") { result = x + y }
else if (currentOp[1] === "^") { result = Math.pow(x, y) }
}
output.push(result);
}
}
} else {
output.push(num);
}
}
}
(2+3)*5
2 3 + 5 *
w odwrotnej
notacji polskiej
75 ms
~13fps
ODWROTNA NOTACJA POLSKA
for(let i = 0; i < 10000; i++) {
let stack = [];
let str = '12 2 3 4 * 10 5 / + * +';
str = str.split(" ");
let output = [];
let currentOp = [];
let num = 0;
let string;
for (let i in str) {
num = parseInt(str[i]);
if (isNaN(num)) {
string = num.toString();
stack.push(str[i]);
if (str.length > 0) {
if (isNaN(str[i])) {
output.push(stack.pop());
currentOp = [];
for (let i = 0; i < 3; i++) {
currentOp.push(output.pop());
this.popped = currentOp.reverse();
let x = currentOp[0];
let y = currentOp[2];
this.result = 0;
if (currentOp[1] === "*") { result = x * y }
else if (currentOp[1] === "/") { result = x / y }
else if (currentOp[1] === "-") { result = x - y }
else if (currentOp[1] === "+") { result = x + y }
else if (currentOp[1] === "^") { result = Math.pow(x, y) }
}
output.push(result);
}
}
} else {
output.push(num);
}
}
}
15.
for (let i= 0; i < 10000; i++) {
let stack = [];
let str = '12 2 3 4 * 10 5 / + * +';
str = str.split(" ");
let output = [];
let currentOp = [];
let num = 0;
let string;
for (let i in str) {
num = parseInt(str[i]);
if (isNaN(num)) {
string = num.toString();
stack.push(str[i]);
if (str.length > 0) {
if (isNaN(str[i])) {
output.push(stack.pop());
currentOp = [];
for (let i = 0; i < 3; i++) {
currentOp.push(output.pop());
this.popped = currentOp.reverse();
let x = currentOp[0];
let y = currentOp[2];
this.result = 0;
if (currentOp[1] === "*") { result = x * y }
else if (currentOp[1] === "/") { result = x / y }
else if (currentOp[1] === "-") { result = x - y }
else if (currentOp[1] === "+") { result = x + y }
else if (currentOp[1] === "^") { result = Math.pow(x, y) }
}
output.push(result);
}
}
} else {
output.push(num);
}
}
}
STRICT MODE
’use strict’;
16.
for (let i= 0; i < 10000; i++) {
let stack = [];
let str = '12 2 3 4 * 10 5 / + * +';
str = str.split(" ");
let output = [];
let currentOp = [];
let num = 0;
let string;
for (let i in str) {
num = parseInt(str[i]);
if (isNaN(num)) {
string = num.toString();
stack.push(str[i]);
if (str.length > 0) {
if (isNaN(str[i])) {
output.push(stack.pop());
currentOp = [];
for (let i = 0; i < 3; i++) {
currentOp.push(output.pop());
this.popped = currentOp.reverse();
let x = currentOp[0];
let y = currentOp[2];
this.result = 0;
if (currentOp[1] === "*") { result = x * y }
else if (currentOp[1] === "/") { result = x / y }
else if (currentOp[1] === "-") { result = x - y }
else if (currentOp[1] === "+") { result = x + y }
else if (currentOp[1] === "^") { result = Math.pow(x, y) }
}
output.push(result);
}
}
} else {
output.push(num);
}
}
}
STRICT MODE
’use strict’;
’use strict’;
for (leti = 0; i < 10000; i++) {
let stack = [];
let str = '12 2 3 4 * 10 5 / + * +';
str = str.split(" ");
let output = [];
let currentOp = [];
let num = 0;
let string;
for (let i in str) {
num = parseInt(str[i]);
if (isNaN(num)) {
string = num.toString();
stack.push(str[i]);
if (str.length > 0) {
if (isNaN(str[i])) {
output.push(stack.pop());
currentOp = [];
for (let i = 0; i < 3; i++) {
currentOp.push(output.pop());
this.popped = currentOp.reverse();
let x = currentOp[0];
let y = currentOp[2];
this.result = 0;
if (currentOp[1] === "*") { result = x * y }
else if (currentOp[1] === "/") { result = x / y }
else if (currentOp[1] === "-") { result = x - y }
else if (currentOp[1] === "+") { result = x + y }
else if (currentOp[1] === "^") { result = Math.pow(x, y) }
}
output.push(result);
}
}
} else {
output.push(num);
}
}
}
ODWROTNA NOTACJA POLSKA
20.
’use strict’;
for (leti = 0; i < 10000; i++) {
let stack = [];
let str = '12 2 3 4 * 10 5 / + * +';
str = str.split(" ");
let output = [];
let currentOp = [];
let num = 0;
let string;
for (let i in str) {
num = parseInt(str[i]);
if (isNaN(num)) {
string = num.toString();
stack.push(str[i]);
if (str.length > 0) {
if (isNaN(str[i])) {
output.push(stack.pop());
currentOp = [];
for (let i = 0; i < 3; i++) {
currentOp.push(output.pop());
this.popped = currentOp.reverse();
let x = currentOp[0];
let y = currentOp[2];
this.result = 0;
if (currentOp[1] === "*") { result = x * y }
else if (currentOp[1] === "/") { result = x / y }
else if (currentOp[1] === "-") { result = x - y }
else if (currentOp[1] === "+") { result = x + y }
else if (currentOp[1] === "^") { result = Math.pow(x, y) }
}
output.push(result);
}
}
} else {
output.push(num);
}
}
}
ODWROTNA NOTACJA POLSKA
REFACTORING ❤
21.
’use strict’;
for (leti = 0; i < 10000; i++) {
let numArray = [];
let input = '2 7 + 3 / 14 3 - 4 * + 2 /';
let toBeInterpeted = input.split(' ');
let isOperator = (val) => {
if (val == '*' || val == '/' || val == '^' || val == '+' || val == '-') { return true; }
return false;
}
let performOperation = (symbol, val2, val1) => {
switch(symbol) {
case '*':
return val1 * val2;
case '/':
return val1 / val2;
case '+':
return +val1 + +val2;
case '-':
return +val1 - +val2;
case '^':
return Math.pow(val1, val2);
}
}
for(let i = 0; i < toBeInterpeted.length; i++) {
let num = toBeInterpeted[i];
let isOperatorSymbol = isOperator(num);
if (isOperatorSymbol) {
let value2 = numArray.pop();
let value1 = numArray.pop();
numArray.push(performOperation(num, value1, value2));
} else {
numArray.push(num);
}
}
}
DELEGACJA ZADAŃ DO DEDYKOWANYCH FUNKCJI
22.
’use strict’;
for (leti = 0; i < 10000; i++) {
let numArray = [];
let input = '2 7 + 3 / 14 3 - 4 * + 2 /';
let toBeInterpeted = input.split(' ');
let isOperator = (val) => {
if (val == '*' || val == '/' || val == '^' || val == '+' || val == '-') { return true; }
return false;
}
let performOperation = (symbol, val2, val1) => {
switch(symbol) {
case '*':
return val1 * val2;
case '/':
return val1 / val2;
case '+':
return +val1 + +val2;
case '-':
return +val1 - +val2;
case '^':
return Math.pow(val1, val2);
}
}
for(let i = 0; i < toBeInterpeted.length; i++) {
let num = toBeInterpeted[i];
let isOperatorSymbol = isOperator(num);
if (isOperatorSymbol) {
let value2 = numArray.pop();
let value1 = numArray.pop();
numArray.push(performOperation(num, value1, value2));
} else {
numArray.push(num);
}
}
}
DELEGACJA ZADAŃ DO DEDYKOWANYCH FUNKCJI
23.
DELEGACJA ZADAŃ DODEDYKOWANYCH FUNKCJI - PORÓWNANIE
13.72 ops/s 41.59 ops/s
PODSUMOWANIE - ILETERAZ TRWA CAŁA OPERACJA?
~3.5 ms(przyspieszenie o ~2050%)
54.
'USE STRICT';
•kompilator niemusi się „domyślać”
•mniejsza szansa na głupi błąd
•niewielki skok wydajności
DELEGACJA ZADAŃ DO
DEDYKOWANYCH FUNKCJI
•czystszy kod
•znaczny skok wydajności
IF/ELSE
•mikrooptymalizacja - do poświęcenia w celu
zwiększenia czytelności kodu
•niewielki skok wydajności
OGRANICZ LICZBĘ
ZMIENNYCH
•potrafi zmniejszyć czytelność kodu
•bardzo duży skok wydajności
PRZENIEŚ STAŁE Z PĘTLI
OBLICZENIOWEJ
•zwiększa „reużywalność” kodu
•duży skok wydajności
TL;DR
LINKI
•https://mythbusters.js.org/
56.
DZIĘKUJĘ
Michał Kostrzyński
Frontend Developer
michal.kostrzynski@vml.com
linkedin.com/in/michalkostrzynski
Zawartośćniniejszej prezentacji, a w szczególności koncepcje i sposób prezentacji treści, stanowią własność intelektualną VML Poland, chronioną prawem zgodnie
z ustawą z dnia 4 lutego 1994 r. o ochronie praw autorskich i praw pokrewnych. Wykorzystanie całości lub części niniejszego utworu w jakichkolwiek celach wymaga
pisemnej zgody właściciela. Niniejsza oferta zachowuje ważność przez okres 3 miesięcy od daty otrzymania.
http://frontowo.pl
https://joind.in/event/codetecon/