From 7307f9119308131f67a6e6fa499b71ae5589074f Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Thu, 26 Sep 2019 11:21:02 +0200 Subject: [PATCH] Performance Improvements: Shutting Down Floating Objects when only charts are visible. --- Animation.js | 42 +++++++++++++++++++++++++++++++++++++---- Spaces/CockpitSpace.js | 2 ++ Spaces/FloatingSpace.js | 10 ++++++---- Spaces/StrategySpace.js | 1 - 4 files changed, 46 insertions(+), 9 deletions(-) diff --git a/Animation.js b/Animation.js index c5a53041d..355d5633a 100644 --- a/Animation.js +++ b/Animation.js @@ -69,15 +69,48 @@ function newAnimation () { /* We loop through the callback functions collections and execute them all. */ - callBackFunctions.forEach(function (callBackFunction) { - callBackFunction() - }) + let performanceMap = new Map() + let totalTimeConsumed = 0 + let totalElements = 0 + let chanceToExecute + let randomNumber + let mustExecute + + for (const [key, callBackFunction] of callBackFunctions.entries()) { + switch (key) { + case 'Floating Space Physics': + chanceToExecute = 100 + randomNumber = Math.random() * 100 * chanceToExecute + if (randomNumber > 100 - chanceToExecute) { mustExecute = true } else { mustExecute = false } + break + default: { + mustExecute = true + } + + } + if (mustExecute === true) { + let t0 = performance.now() + callBackFunction() + let t1 = performance.now() + let timeConsumed = t1 - t0 + performanceMap.set(key, timeConsumed) + totalTimeConsumed = totalTimeConsumed + timeConsumed + totalElements++ + } + } + + /* Performance Check */ + console.clear() + for (const [key, timeConsumed] of performanceMap.entries()) { + let percentage = timeConsumed * 100 / totalTimeConsumed + console.log(key, percentage.toFixed(2)) + } } else { browserCanvas.width = 1 browserCanvas.height = 1 } - /* We request the next frame to be drawn, and stablishing a loop */ + /* We request the next frame to be drawn, and stablishing a loop */ /* Use this code for full animation speed. */ animationLoopHandle = window.requestAnimationFrame(animationLoop) @@ -104,3 +137,4 @@ function newAnimation () { browserCanvasContext.fill() } } + diff --git a/Spaces/CockpitSpace.js b/Spaces/CockpitSpace.js index 86fc1c821..81836e78e 100644 --- a/Spaces/CockpitSpace.js +++ b/Spaces/CockpitSpace.js @@ -119,8 +119,10 @@ thisObject.container.frame.position.y = browserCanvas.height - COCKPIT_SPACE_HEIGHT thisObject.status = 'BOTTOM' canvas.strategySpace.makeInvisible() + canvas.floatingSpace.makeInvisible() } else { canvas.strategySpace.makeVisible() + canvas.floatingSpace.makeVisible() } if (thisObject.container.frame.position.y < browserCanvas.height * 0.5 / 100) { diff --git a/Spaces/FloatingSpace.js b/Spaces/FloatingSpace.js index 1deab5a25..e6a5d4cd3 100644 --- a/Spaces/FloatingSpace.js +++ b/Spaces/FloatingSpace.js @@ -137,8 +137,10 @@ function newFloatingSpace () { } function physics () { - browserZoomPhysics() - thisObject.floatingLayer.physics() + if (visible === true) { + browserZoomPhysics() + thisObject.floatingLayer.physics() + } } function browserZoomPhysics () { @@ -153,9 +155,8 @@ function newFloatingSpace () { function draw () { if (visible === true) { drawBackground() + thisObject.floatingLayer.draw() } - - thisObject.floatingLayer.draw() } function drawBackground () { @@ -168,3 +169,4 @@ function newFloatingSpace () { browserCanvasContext.fill() } } + diff --git a/Spaces/StrategySpace.js b/Spaces/StrategySpace.js index d940f2325..6a69efd76 100644 --- a/Spaces/StrategySpace.js +++ b/Spaces/StrategySpace.js @@ -187,7 +187,6 @@ function newStrategySpace () { } function makeVisible () { - canvas.floatingSpace.makeVisible() visible = true }