SlideShare a Scribd company logo
1 of 56
Download to read offline
JAVASCRIPT I WYDAJNOŚĆ
TO SIĘ SPINA?
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
01WPROWADZENIE
GLOBALDIGITALAGENCY
1,900+EMPLOYEES
ATLANTA
BEIJING
BOGOTÁ
BOSTON
CAPE TOWN
CHICAGO
GUANGZHOU
JAKARTA
JOHANNESBURG
KALAMAZOO
KANSAS CITY
KRAKOW
LONDON
MILAN
MUMBAI
NEW YORK
SÃO PAULO
SEATTLE
SHANGHAI
SINGAPORE
SYDNEY
TOKYO
WARSAW
WHITE SALMON
PO CO TA PREZENTACJA
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);
}
}
}
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
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
CO MOŻNA Z TYM ZROBIĆ?
02RESTRYKCJE SĄ COOL
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);
}
}
}
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’;
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’;
03PO CO SIĘ POWTARZAĆ
’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
’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 ❤
’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
’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
DELEGACJA ZADAŃ DO DEDYKOWANYCH FUNKCJI - PORÓWNANIE
13.72 ops/s 41.59 ops/s
04JEŚLI, A CO JEŚLI NIE?
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);
}
}
}
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):
//…
}
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):
//…
}
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);
}
}
}
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);
}
}
}
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); }
}
05ZA DUŻO GADAM
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);
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);
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);
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);
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);
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);
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);
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]);
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]);
OGRANICZAMY ILOŚĆ ZMIENNYCH - PORÓWNANIE
41.11 ops/s 200 ops/s
06PRZESUŃ TO, PRZESUŃ TAMTO
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’;
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’;
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); }
}
const
const
const
const
’use strict’;
WYRZUCAMY STAŁE Z PĘTLI OBLICZENIOWEJ - PORÓWNANIE
204 ops/s 279 ops/s
~75 ms
PODSUMOWANIE - ILE TERAZ TRWA CAŁA OPERACJA?
PODSUMOWANIE - ILE TERAZ TRWA CAŁA OPERACJA?
~3.5 ms(przyspieszenie o ~2050%)
'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/
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/

More Related Content

What's hot

What's hot (19)

Ngon ngu lap trinh
Ngon ngu lap trinhNgon ngu lap trinh
Ngon ngu lap trinh
 
javascript networking
javascript networkingjavascript networking
javascript networking
 
Ugd9 c 7644
Ugd9 c 7644Ugd9 c 7644
Ugd9 c 7644
 
Tomografi Delay Time Sederhana
Tomografi Delay Time SederhanaTomografi Delay Time Sederhana
Tomografi Delay Time Sederhana
 
Practica 4 errores
Practica 4 erroresPractica 4 errores
Practica 4 errores
 
Prueba de montecarlo
Prueba de montecarloPrueba de montecarlo
Prueba de montecarlo
 
Info clasa
Info clasaInfo clasa
Info clasa
 
Yohan jacobi gaussseidel_analisis
Yohan jacobi gaussseidel_analisisYohan jacobi gaussseidel_analisis
Yohan jacobi gaussseidel_analisis
 
Programación funcional en Haskell
Programación funcional en HaskellProgramación funcional en Haskell
Programación funcional en Haskell
 
Juego del gato
Juego del gatoJuego del gato
Juego del gato
 
Vcs21
Vcs21Vcs21
Vcs21
 
Alejandro
AlejandroAlejandro
Alejandro
 
Bt c cpp_0021
Bt c cpp_0021Bt c cpp_0021
Bt c cpp_0021
 
Pendekatan Inversi Linier dengan Matriks Jacobi pada Kasus Perhitungan Hipose...
Pendekatan Inversi Linier dengan Matriks Jacobi pada Kasus Perhitungan Hipose...Pendekatan Inversi Linier dengan Matriks Jacobi pada Kasus Perhitungan Hipose...
Pendekatan Inversi Linier dengan Matriks Jacobi pada Kasus Perhitungan Hipose...
 
Dsa 1
Dsa 1Dsa 1
Dsa 1
 
[KOSSA] C++ Programming - 14th Study - template
[KOSSA] C++ Programming - 14th Study - template[KOSSA] C++ Programming - 14th Study - template
[KOSSA] C++ Programming - 14th Study - template
 
Sbaw090630
Sbaw090630Sbaw090630
Sbaw090630
 
An introduction to functional programming with Go [redux]
An introduction to functional programming with Go [redux]An introduction to functional programming with Go [redux]
An introduction to functional programming with Go [redux]
 
python-geohex
python-geohexpython-geohex
python-geohex
 

Javascript i wydajność - czy to się spina?

  • 2.
  • 3. 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
  • 5.
  • 7. PO CO TA PREZENTACJA
  • 8. 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
  • 11. CO MOŻNA Z TYM ZROBIĆ?
  • 12.
  • 14. 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’;
  • 18.
  • 19. ’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
  • 20. ’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 ❤
  • 21. ’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
  • 22. ’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
  • 23. DELEGACJA ZADAŃ DO DEDYKOWANYCH FUNKCJI - PORÓWNANIE 13.72 ops/s 41.59 ops/s
  • 24. 04JEŚLI, A CO JEŚLI NIE?
  • 25.
  • 26. 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); } } }
  • 27. 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): //… }
  • 28. 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): //… }
  • 29. 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); } } }
  • 30. 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); } } }
  • 31. 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); } }
  • 33.
  • 34. 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);
  • 35. 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);
  • 36. 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);
  • 37. 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);
  • 38. 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);
  • 39. 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);
  • 40. 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);
  • 41. 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]);
  • 42. 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]);
  • 43. OGRANICZAMY ILOŚĆ ZMIENNYCH - PORÓWNANIE 41.11 ops/s 200 ops/s
  • 44.
  • 45.
  • 47.
  • 48. 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’;
  • 49. 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’;
  • 50. 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); } } const const const const ’use strict’;
  • 51. WYRZUCAMY STAŁE Z PĘTLI OBLICZENIOWEJ - PORÓWNANIE 204 ops/s 279 ops/s
  • 52. ~75 ms PODSUMOWANIE - ILE TERAZ TRWA CAŁA OPERACJA?
  • 53. PODSUMOWANIE - ILE TERAZ TRWA CAŁA OPERACJA? ~3.5 ms(przyspieszenie o ~2050%)
  • 54. '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/
  • 55.
  • 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/