javascript i wydajność - czy to się spina?
TRANSCRIPT
![Page 1: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/1.jpg)
JAVASCRIPT I WYDAJNOŚĆ TO SIĘ SPINA?
![Page 2: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/2.jpg)
![Page 3: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/3.jpg)
AGENDA 01 Wprowadzenie 02 Restrykcje są cool 03 Po co się powtarzać? 04 Jeśli, a co jeśli nie? 05 Za dużo gadam 06 Przesuń to, przesuń tamto 07 TL;DR
![Page 4: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/4.jpg)
01WPROWADZENIE
![Page 5: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/5.jpg)
![Page 6: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/6.jpg)
GLOBAL DIGITAL AGENCY1,900+ EMPLOYEES
ATLANTABEIJINGBOGOTÁBOSTONCAPE TOWNCHICAGO
GUANGZHOUJAKARTAJOHANNESBURGKALAMAZOOKANSAS CITYKRAKOW
LONDONMILANMUMBAINEW YORKSÃO PAULOSEATTLE
SHANGHAISINGAPORESYDNEYTOKYOWARSAWWHITE SALMON
![Page 7: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/7.jpg)
PO CO TA PREZENTACJA
![Page 8: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/8.jpg)
PRZYKŁADOWY KOD - ODWROTNA NOTACJA POLSKAfor (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); } } }
![Page 9: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/9.jpg)
PRZYKŁADOWY KOD - ODWROTNA NOTACJA POLSKAfor (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
![Page 10: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/10.jpg)
PRZYKŁADOWY KOD - ODWROTNA NOTACJA POLSKAfor (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
![Page 11: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/11.jpg)
CO MOŻNA Z TYM ZROBIĆ?
![Page 12: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/12.jpg)
![Page 13: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/13.jpg)
02RESTRYKCJE SĄ COOL
![Page 14: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/14.jpg)
ODWROTNA NOTACJA POLSKAfor (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); } } }
![Page 15: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/15.jpg)
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’;
![Page 16: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/16.jpg)
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’;
![Page 17: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/17.jpg)
03PO CO SIĘ POWTARZAĆ
![Page 18: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/18.jpg)
![Page 19: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/19.jpg)
’use strict’; 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); } } }
ODWROTNA NOTACJA POLSKA
![Page 20: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/20.jpg)
’use strict’; 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); } } }
ODWROTNA NOTACJA POLSKA
REFACTORING ❤
![Page 21: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/21.jpg)
’use strict’; for (let i = 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
![Page 22: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/22.jpg)
’use strict’; for (let i = 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
![Page 23: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/23.jpg)
DELEGACJA ZADAŃ DO DEDYKOWANYCH FUNKCJI - PORÓWNANIE
13.72 ops/s 41.59 ops/s
![Page 24: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/24.jpg)
04JEŚLI, A CO JEŚLI NIE?
![Page 25: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/25.jpg)
![Page 26: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/26.jpg)
WYŻSZOŚĆ IF/ELSE
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); } }
let isOperator = (val) => { if (val == '*' || val == '/' || val == '^' || val == '+' || val == '-') { return true; } return false; }
let numArray = []; let input = '2 7 + 3 / 14 3 - 4 * + 2 /'; let toBeInterpeted = input.split(' ');
’use strict’; for (let i = 0; i < 10000; i++) {
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); } } }
![Page 27: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/27.jpg)
WYŻSZOŚĆ IF/ELSE
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); } }
let isOperator = (val) => { if (val == '*' || val == '/' || val == '^' || val == '+' || val == '-') { return true; } return false; }
let numArray = []; let input = '2 7 + 3 / 14 3 - 4 * + 2 /'; let toBeInterpeted = input.split(' ');
’use strict’; for (let i = 0; i < 10000; i++) {
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); } } }
var x = true;var y = false;switch(true) {
case (x || y)://…
}
![Page 28: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/28.jpg)
WYŻSZOŚĆ IF/ELSE
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); } }
let isOperator = (val) => { if (val == '*' || val == '/' || val == '^' || val == '+' || val == '-') { return true; } return false; }
let numArray = []; let input = '2 7 + 3 / 14 3 - 4 * + 2 /'; let toBeInterpeted = input.split(' ');
’use strict’; for (let i = 0; i < 10000; i++) {
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); } } }
var x = true;var y = false;switch(true) {
case (x || y)://…
}
![Page 29: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/29.jpg)
WYŻSZOŚĆ IF/ELSE
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); } }
let isOperator = (val) => { if (val == '*' || val == '/' || val == '^' || val == '+' || val == '-') { return true; } return false; }
let numArray = []; let input = '2 7 + 3 / 14 3 - 4 * + 2 /'; let toBeInterpeted = input.split(' ');
’use strict’; for (let i = 0; i < 10000; i++) {
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); } } }
![Page 30: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/30.jpg)
WYŻSZOŚĆ IF/ELSE
let isOperator = (val) => { if (val == '*' || val == '/' || val == '^' || val == '+' || val == '-') { return true; } return false; }
let numArray = []; let input = '2 7 + 3 / 14 3 - 4 * + 2 /'; let toBeInterpeted = input.split(' ');
’use strict’; for (let i = 0; i < 10000; i++) {
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); } } }
![Page 31: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/31.jpg)
WYŻSZOŚĆ IF/ELSE
let isOperator = (val) => { if (val == '*' || val == '/' || val == '^' || val == '+' || val == '-') { return true; } return false; }
let numArray = []; let input = '2 7 + 3 / 14 3 - 4 * + 2 /'; let toBeInterpeted = input.split(' ');
’use strict’; for (let i = 0; i < 10000; i++) {
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); } } }
let performOperation = (symbol, val2, val1) => {if (symbol == '*') { return val1*val2; }else if (symbol == '/') { return val1/val2; }else if (symbol == '+') { return +val1 + +val2; }else if (symbol == '-') { return +val1 - +val2; }else if (symbol == '^') { return Math.pow(val1, val2); }
}
![Page 32: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/32.jpg)
05ZA DUŻO GADAM
![Page 33: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/33.jpg)
![Page 34: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/34.jpg)
OGRANICZAMY ILOŚĆ ZMIENNYCH
let isOperator = (val) => { if (val == '*' || val == '/' || val == '^' || val == '+' || val == '-') { return true; } return false; }
let numArray = []; let input = '2 7 + 3 / 14 3 - 4 * + 2 /'; let toBeInterpeted = input.split(' ');
’use strict’; for (let i = 0; i < 10000; i++) {
} else {
} } }
if (symbol == '*') { return val1*val2; } else if (symbol == '/') { return val1/val2; } else if (symbol == '+') { return +val1 + +val2; } else if (symbol == '-') { return +val1 - +val2; } else if (symbol == '^') { return Math.pow(val1, val2); } }
for(let i = 0; i < toBeInterpeted.length; i++) { let num = toBeInterpeted[i]; let isOperatorSymbol = isOperator(num);
if (isOperatorSymbol) {
let performOperation = (symbol,
let value2 = numArray.pop(); let value1 = numArray.pop(); numArray.push(performOperation(num,
numArray.push(
val2, val1) => {
value1, value2));
num);
![Page 35: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/35.jpg)
OGRANICZAMY ILOŚĆ ZMIENNYCH
let isOperator = (val) => { if (val == '*' || val == '/' || val == '^' || val == '+' || val == '-') { return true; } return false; }
let numArray = []; let input = '2 7 + 3 / 14 3 - 4 * + 2 /'; let toBeInterpeted = input.split(' ');
’use strict’; for (let i = 0; i < 10000; i++) {
} else {
} } }
if (symbol == '*') { return val1*val2; } else if (symbol == '/') { return val1/val2; } else if (symbol == '+') { return +val1 + +val2; } else if (symbol == '-') { return +val1 - +val2; } else if (symbol == '^') { return Math.pow(val1, val2); } }
for(let i = 0; i < toBeInterpeted.length; i++) {
if (isOperatorSymbol) {
let performOperation = (symbol,
let value2 = numArray.pop(); let value1 = numArray.pop(); numArray.push(performOperation(num,
numArray.push(
val2, val1) => {
value1, value2));
num);
![Page 36: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/36.jpg)
OGRANICZAMY ILOŚĆ ZMIENNYCH
let isOperator = (val) => { if (val == '*' || val == '/' || val == '^' || val == '+' || val == '-') { return true; } return false; }
let numArray = []; let input = '2 7 + 3 / 14 3 - 4 * + 2 /'; let toBeInterpeted = input.split(' ');
’use strict’; for (let i = 0; i < 10000; i++) {
} else {
} } }
if (symbol == '*') { return val1*val2; } else if (symbol == '/') { return val1/val2; } else if (symbol == '+') { return +val1 + +val2; } else if (symbol == '-') { return +val1 - +val2; } else if (symbol == '^') { return Math.pow(val1, val2); } }
for(let i = 0; i < toBeInterpeted.length; i++) {
let performOperation = (symbol,
let value2 = numArray.pop(); let value1 = numArray.pop(); numArray.push(performOperation(num,
numArray.push(
if (isOperator(toBeInterpreted[i])) {
val2, val1) => {
value1, value2));
num);
![Page 37: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/37.jpg)
OGRANICZAMY ILOŚĆ ZMIENNYCH
let isOperator = (val) => { if (val == '*' || val == '/' || val == '^' || val == '+' || val == '-') { return true; } return false; }
let numArray = []; let input = '2 7 + 3 / 14 3 - 4 * + 2 /'; let toBeInterpeted = input.split(' ');
’use strict’; for (let i = 0; i < 10000; i++) {
} else {
} } }
if (symbol == '*') { return val1*val2; } else if (symbol == '/') { return val1/val2; } else if (symbol == '+') { return +val1 + +val2; } else if (symbol == '-') { return +val1 - +val2; } else if (symbol == '^') { return Math.pow(val1, val2); } }
for(let i = 0; i < toBeInterpeted.length; i++) {
let performOperation = (symbol,
let value2 = numArray.pop(); let value1 = numArray.pop(); numArray.push(performOperation(num,
numArray.push(
if (isOperator(toBeInterpreted[i])) {
val1, val2) => {
value1, value2));
num);
![Page 38: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/38.jpg)
OGRANICZAMY ILOŚĆ ZMIENNYCH
let isOperator = (val) => { if (val == '*' || val == '/' || val == '^' || val == '+' || val == '-') { return true; } return false; }
let numArray = []; let input = '2 7 + 3 / 14 3 - 4 * + 2 /'; let toBeInterpeted = input.split(' ');
’use strict’; for (let i = 0; i < 10000; i++) {
} else {
} } }
if (symbol == '*') { return val1*val2; } else if (symbol == '/') { return val1/val2; } else if (symbol == '+') { return +val1 + +val2; } else if (symbol == '-') { return +val1 - +val2; } else if (symbol == '^') { return Math.pow(val1, val2); } }
for(let i = 0; i < toBeInterpeted.length; i++) {
let performOperation = (symbol,
numArray.push(performOperation(num,
numArray.push(
if (isOperator(toBeInterpreted[i])) {
val1, val2) => {
value1, value2));
num);
![Page 39: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/39.jpg)
OGRANICZAMY ILOŚĆ ZMIENNYCH
let isOperator = (val) => { if (val == '*' || val == '/' || val == '^' || val == '+' || val == '-') { return true; } return false; }
let numArray = []; let input = '2 7 + 3 / 14 3 - 4 * + 2 /'; let toBeInterpeted = input.split(' ');
’use strict’; for (let i = 0; i < 10000; i++) {
} else {
} } }
if (symbol == '*') { return val1*val2; } else if (symbol == '/') { return val1/val2; } else if (symbol == '+') { return +val1 + +val2; } else if (symbol == '-') { return +val1 - +val2; } else if (symbol == '^') { return Math.pow(val1, val2); } }
for(let i = 0; i < toBeInterpeted.length; i++) {
let performOperation = (symbol,
numArray.push(performOperation(num,
numArray.push(
if (isOperator(toBeInterpreted[i])) {
val1, val2) => {
numArray.pop(), numArray.pop()));
num);
![Page 40: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/40.jpg)
OGRANICZAMY ILOŚĆ ZMIENNYCH
let isOperator = (val) => { if (val == '*' || val == '/' || val == '^' || val == '+' || val == '-') { return true; } return false; }
let numArray = []; let input = '2 7 + 3 / 14 3 - 4 * + 2 /'; let toBeInterpeted = input.split(' ');
’use strict’; for (let i = 0; i < 10000; i++) {
} else {
} } }
if (symbol == '*') { return val1*val2; } else if (symbol == '/') { return val1/val2; } else if (symbol == '+') { return +val1 + +val2; } else if (symbol == '-') { return +val1 - +val2; } else if (symbol == '^') { return Math.pow(val1, val2); } }
for(let i = 0; i < toBeInterpeted.length; i++) {
let performOperation = (symbol,
numArray.push(performOperation(num,
numArray.push(
if (isOperator(toBeInterpreted[i])) {
val1, val2) => {
numArray.pop(), numArray.pop()));
num);
![Page 41: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/41.jpg)
OGRANICZAMY ILOŚĆ ZMIENNYCH
let isOperator = (val) => { if (val == '*' || val == '/' || val == '^' || val == '+' || val == '-') { return true; } return false; }
let numArray = []; let input = '2 7 + 3 / 14 3 - 4 * + 2 /'; let toBeInterpeted = input.split(' ');
’use strict’; for (let i = 0; i < 10000; i++) {
} else {
} } }
if (symbol == '*') { return val1*val2; } else if (symbol == '/') { return val1/val2; } else if (symbol == '+') { return +val1 + +val2; } else if (symbol == '-') { return +val1 - +val2; } else if (symbol == '^') { return Math.pow(val1, val2); } }
for(let i = 0; i < toBeInterpeted.length; i++) {
let performOperation = (symbol,
numArray.push(performOperation(num,
numArray.push(
if (isOperator(toBeInterpreted[i])) {
val1, val2) => {
numArray.pop(), numArray.pop()));
toBeInterpreted[i]);
![Page 42: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/42.jpg)
OGRANICZAMY ILOŚĆ ZMIENNYCH
let isOperator = (val) => { if (val == '*' || val == '/' || val == '^' || val == '+' || val == '-') { return true; } return false; }
let numArray = []; let input = '2 7 + 3 / 14 3 - 4 * + 2 /'; let toBeInterpeted = input.split(' ');
’use strict’; for (let i = 0; i < 10000; i++) {
} else {
} } }
if (symbol == '*') { return val1*val2; } else if (symbol == '/') { return val1/val2; } else if (symbol == '+') { return +val1 + +val2; } else if (symbol == '-') { return +val1 - +val2; } else if (symbol == '^') { return Math.pow(val1, val2); } }
for(let i = 0; i < toBeInterpeted.length; i++) {
let performOperation = (symbol,
numArray.push(performOperation(num,
numArray.push(
if (isOperator(toBeInterpreted[i])) {
val1, val2) => {
numArray.pop(), numArray.pop()));
toBeInterpreted[i]);
![Page 43: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/43.jpg)
OGRANICZAMY ILOŚĆ ZMIENNYCH - PORÓWNANIE
41.11 ops/s 200 ops/s
![Page 44: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/44.jpg)
![Page 45: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/45.jpg)
![Page 46: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/46.jpg)
06PRZESUŃ TO, PRZESUŃ TAMTO
![Page 47: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/47.jpg)
![Page 48: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/48.jpg)
WYRZUCAMY STAŁE Z PĘTLI OBLICZENIOWEJ
isOperator = (val) => { if (val == '*' || val == '/' || val == '^' || val == '+' || val == '-') { return true; } return false; }
input = '2 7 + 3 / 14 3 - 4 * + 2 /'; toBeInterpeted = input.split(' ');
for (let i = 0; i < 10000; i++) { let numArray = [];
for(let i = 0; i < toBeInterpeted.length; i++) { if (isOperator(toBeInterpreted[i])) { numArray.push(performOperation(num, numArray.pop(), numArray.pop())); } else { numArray.push(toBeInterpreted[i]); } } }
performOperation = (symbol, val1, val2) => { if (symbol == '*') { return val1*val2; } else if (symbol == '/') { return val1/val2; } else if (symbol == '+') { return +val1 + +val2; } else if (symbol == '-') { return +val1 - +val2; } else if (symbol == '^') { return Math.pow(val1, val2); } }
let let
let
let
’use strict’;
![Page 49: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/49.jpg)
WYRZUCAMY STAŁE Z PĘTLI OBLICZENIOWEJ
isOperator = (val) => { if (val == '*' || val == '/' || val == '^' || val == '+' || val == '-') { return true; } return false; }
input = '2 7 + 3 / 14 3 - 4 * + 2 /'; toBeInterpeted = input.split(' ');
for (let i = 0; i < 10000; i++) { let numArray = []; for(let i = 0; i < toBeInterpeted.length; i++) { if (isOperator(toBeInterpreted[i])) { numArray.push(performOperation(num, numArray.pop(), numArray.pop())); } else { numArray.push(toBeInterpreted[i]); } } }
performOperation = (symbol, val1, val2) => { if (symbol == '*') { return val1*val2; } else if (symbol == '/') { return val1/val2; } else if (symbol == '+') { return +val1 + +val2; } else if (symbol == '-') { return +val1 - +val2; } else if (symbol == '^') { return Math.pow(val1, val2); } }
letlet
let
let
’use strict’;
![Page 50: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/50.jpg)
WYRZUCAMY STAŁE Z PĘTLI OBLICZENIOWEJ
isOperator = (val) => { if (val == '*' || val == '/' || val == '^' || val == '+' || val == '-') { return true; } return false; }
input = '2 7 + 3 / 14 3 - 4 * + 2 /'; toBeInterpeted = input.split(' ');
for (let i = 0; i < 10000; i++) { let numArray = []; for(let i = 0; i < toBeInterpeted.length; i++) { if (isOperator(toBeInterpreted[i])) { numArray.push(performOperation(num, numArray.pop(), numArray.pop())); } else { numArray.push(toBeInterpreted[i]); } } }
performOperation = (symbol, val1, val2) => { if (symbol == '*') { return val1*val2; } else if (symbol == '/') { return val1/val2; } else if (symbol == '+') { return +val1 + +val2; } else if (symbol == '-') { return +val1 - +val2; } else if (symbol == '^') { return Math.pow(val1, val2); } }
constconst
const
const
’use strict’;
![Page 51: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/51.jpg)
WYRZUCAMY STAŁE Z PĘTLI OBLICZENIOWEJ - PORÓWNANIE
204 ops/s 279 ops/s
![Page 52: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/52.jpg)
~75 ms
PODSUMOWANIE - ILE TERAZ TRWA CAŁA OPERACJA?
![Page 53: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/53.jpg)
PODSUMOWANIE - ILE TERAZ TRWA CAŁA OPERACJA?
~3.5 ms(przyspieszenie o ~2050%)
![Page 54: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/54.jpg)
'USE STRICT';
•kompilator nie musi 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/
![Page 55: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/55.jpg)
![Page 56: Javascript i wydajność - czy to się spina?](https://reader031.vdocuments.net/reader031/viewer/2022030309/58f101f21a28abf65c8b462f/html5/thumbnails/56.jpg)
DZIĘKUJĘMichał Kostrzyński Frontend Developer [email protected] 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/