51

Performance optimisation in javascript

  • Upload
    -

  • View
    274

  • Download
    4

Embed Size (px)

DESCRIPTION

JS, performance, pics.io, binary, lightroom in browser

Citation preview

Page 1: Performance optimisation in javascript
Page 2: Performance optimisation in javascript

Кто это?

● Gameloft● WebSpellChecker● DevPro● Pics.io

@blackrabbit99

Page 3: Performance optimisation in javascript
Page 4: Performance optimisation in javascript
Page 5: Performance optimisation in javascript
Page 6: Performance optimisation in javascript
Page 7: Performance optimisation in javascript
Page 8: Performance optimisation in javascript
Page 9: Performance optimisation in javascript
Page 10: Performance optimisation in javascript
Page 11: Performance optimisation in javascript

Причины падения браузеров

● Утечки памяти● Куча http запросов● Слабая конфигурация машины

Page 12: Performance optimisation in javascript
Page 13: Performance optimisation in javascript

UI

Page 14: Performance optimisation in javascript

Trottling

Выполнение метода не чаще одного раза в указанный период, даже если он будет вызван много раз в течение этого периода

Page 15: Performance optimisation in javascript

Debouncing

● Реальный вызов происходит только в случае, если с момента последней попытки прошло время, большее или равное задержке.

● Реальный вызов происходит сразу, а все остальные попытки вызова игнорируются, пока не пройдет время, большее или равное задержке, отсчитанной от времени последней попытки.

Page 16: Performance optimisation in javascript

Вычисления

Page 17: Performance optimisation in javascript

Memoization

Page 18: Performance optimisation in javascript

Memoization

Function.prototype.memoize = function(){var self = this, cache = {};

return function( arg ){if(arg in cache) {

console.log('Cache hit for '+arg);return cache[arg];

} else {console.log('Cache miss for '+arg);return cache[arg] = self( arg );

}}

}

Page 19: Performance optimisation in javascript

Memoization

function hugeCalc(a){ ... }

var memoHugeCalc = hugeCalc.memoize();

memoHugeCalc(1);memoHugeCalc(1); memoHugeCalc(2);

Page 20: Performance optimisation in javascript

Self-defining functions

var defineMeAgain = function () {console.log("First");defineMeAgain = function () {

console.log("Second");};

};

Page 21: Performance optimisation in javascript

Self-defining functions

var secondFun = defineMeAgain;secondFun(); // "Boo!"secondFun(); // "Boo!"defineMeAgain(); // Double boo!defineMeAgain(); // Double boo!

Page 22: Performance optimisation in javascript

Look up tables

Page 23: Performance optimisation in javascript

Целочисленные значения

ctx.drawImage(myImage, 0.3, 0.5)

Page 24: Performance optimisation in javascript

CSS3 transform

Page 25: Performance optimisation in javascript

Nearest-neighbour rendering

Page 26: Performance optimisation in javascript

Не создавайте мусор● Array.slice● Array.splice● Function.bind

Page 27: Performance optimisation in javascript

Микрооптимизация● Use x | 0 instead of Math.floor● Clear arrays with .length = 0 to avoid creating a new Array● Use if .. else over switch● Use TypedArrays for floats or integers (e.g. vectors and matrices)

Page 28: Performance optimisation in javascript

Weak mapsvar map = new WeakMap(), element = jsObject;

map.set(element, {imAssosiatedWith: 'element'});var value = map.get(element);console.log(value);

element = null;

value = map.get(element);console.log(value);

Page 29: Performance optimisation in javascript

Object pool

Page 30: Performance optimisation in javascript

Object pool

Page 31: Performance optimisation in javascript

WebWorkers

Page 32: Performance optimisation in javascript

WebWorkers

● Web workers не могут доступиться к DOM элементам● Web workers ничего не знают о глабольном скоупе, скоуп у них свой● Web workers не знают о alert и confirm● Window, documents также недоступны

Page 33: Performance optimisation in javascript

WebWorkers

Могут запустить потоки паралельно!!!!

Page 34: Performance optimisation in javascript
Page 35: Performance optimisation in javascript
Page 36: Performance optimisation in javascript
Page 37: Performance optimisation in javascript
Page 38: Performance optimisation in javascript
Page 39: Performance optimisation in javascript

Web Workers

● Dedicated Web Worker● Shared Web Worker● Http transport● Error handling● Terminate

Page 40: Performance optimisation in javascript

Parallel.js

var dataToParalel = plugin.prepare(imageSlices.getSliceList(), data);

var p = new Parallel(dataToParalel);

p.map(cb).then(next);

Page 41: Performance optimisation in javascript

Еще проблем

Page 42: Performance optimisation in javascript

API для асинхронной работой с key-value базой данных

IndexedDB

Page 43: Performance optimisation in javascript

var transaction = db.transaction(["history"],IDBTransaction.READ_WRITE);

var put = transaction.objectStore("history").put(blob, "image");

IndexedDB

Page 44: Performance optimisation in javascript

SIMD дает возможность работать с векторами, а не с одиночными значениями

Simd

Page 45: Performance optimisation in javascript

● loat32x4 (C type: __m128): four 32 bit floating point numbers.● uint32x4 (C type: __m128i): four 32 bit unsigned integers.

Simd

Page 46: Performance optimisation in javascript

var a = SIMD.float32x4(1.0, 2.0, 3.0, 4.0);var b = SIMD.float32x4(5.0, 6.0, 7.0, 8.0);var c = SIMD.float32x4.add(a,b);

Simd

Page 47: Performance optimisation in javascript

float32x4.abs(v)float32x4.neg(v)float32x4.sqrt(v)float32x4.add(v, w)float32x4.mul(v, w)float32x4.equal(v, w)

Simd

Page 48: Performance optimisation in javascript

Движок River Trail даст толчек ParallelJS. ParallelArray позволят работать с данными паралельно

ParallelJS

Page 49: Performance optimisation in javascript

ParallelJS

● Array.prototype.mapPar()● Array.prototype.filterPar()● Array.prototype.reducePar()

Page 51: Performance optimisation in javascript

Дякую

QA