|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const nx = require('@nx-js/framework') |
| 4 | +const randomSentence = require('./randomSentence') |
| 5 | +const timer = require('./timer') |
| 6 | + |
| 7 | +let id = 1 |
| 8 | + |
| 9 | +// the app component uses all of the core nx middlewares by default |
| 10 | +// it can be instantiated with <perf-app></perf-app> in HTML |
| 11 | +nx.components.app() |
| 12 | + .use(setup) |
| 13 | + .register('perf-app') |
| 14 | + |
| 15 | +// the performance sensitive component uses the required middlewares only |
| 16 | +// it can be instantiated with <tr is="perf-row"></tr> in HTML |
| 17 | +nx.component({element: 'tr', state: false, isolate: 'middlewares'}) |
| 18 | + .useOnContent(nx.middlewares.observe) |
| 19 | + .useOnContent(nx.middlewares.interpolate) |
| 20 | + .useOnContent(nx.middlewares.events) |
| 21 | + .use(nx.middlewares.attributes) |
| 22 | + .use(nx.middlewares.style) |
| 23 | + .register('perf-row') |
| 24 | + |
| 25 | +// this is a custom middleware to setup the state for the app component |
| 26 | +function setup (elem, state) { |
| 27 | + state.rows = [] |
| 28 | + |
| 29 | + state.add = function add () { |
| 30 | + timer.startMeasure('add') |
| 31 | + for (var i = 0; i < 1000; i++) { |
| 32 | + state.rows.push({ id: id++, label: randomSentence() }) |
| 33 | + } |
| 34 | + timer.stopMeasure() |
| 35 | + } |
| 36 | + |
| 37 | + state.remove = function remove (item) { |
| 38 | + timer.startMeasure('remove') |
| 39 | + const index = state.rows.indexOf(item) |
| 40 | + state.rows.splice(index, 1) |
| 41 | + timer.stopMeasure() |
| 42 | + } |
| 43 | + |
| 44 | + state.select = function select (item) { |
| 45 | + timer.startMeasure('select') |
| 46 | + state.selected = item |
| 47 | + timer.stopMeasure() |
| 48 | + } |
| 49 | + |
| 50 | + state.run = function run () { |
| 51 | + timer.startMeasure('add') |
| 52 | + state.rows = [] |
| 53 | + for (var i = 0; i < 1000; i++) { |
| 54 | + state.rows.push({ id: id++, label: randomSentence() }) |
| 55 | + } |
| 56 | + timer.stopMeasure() |
| 57 | + } |
| 58 | + |
| 59 | + state.update = function update () { |
| 60 | + timer.startMeasure('update') |
| 61 | + for (let i = 0; i < state.rows.length; i++) { |
| 62 | + if (i % 10 === 0) state.rows[i].label += ' !!!' |
| 63 | + } |
| 64 | + timer.stopMeasure() |
| 65 | + } |
| 66 | + |
| 67 | + state.runLots = function runLots () { |
| 68 | + timer.startMeasure('add') |
| 69 | + state.rows = [] |
| 70 | + for (var i = 0; i < 10000; i++) { |
| 71 | + state.rows.push({ id: id++, label: randomSentence() }) |
| 72 | + } |
| 73 | + timer.stopMeasure() |
| 74 | + } |
| 75 | + |
| 76 | + state.clear = function clear () { |
| 77 | + timer.startMeasure('clear') |
| 78 | + state.rows = [] |
| 79 | + timer.stopMeasure() |
| 80 | + } |
| 81 | + |
| 82 | + state.swapRows = function swapRows () { |
| 83 | + timer.startMeasure('swapRows') |
| 84 | + if (10 < state.rows.length) { |
| 85 | + const item4 = state.rows[4] |
| 86 | + const item9 = state.rows[9] |
| 87 | + state.rows[4] = item9 |
| 88 | + state.rows[9] = item4 |
| 89 | + } |
| 90 | + timer.stopMeasure() |
| 91 | + } |
| 92 | +} |
0 commit comments