Skip to content

Commit 19cdc65

Browse files
committed
Reduce object allocation / garbage creation
1 parent e10f7d7 commit 19cdc65

File tree

6 files changed

+109
-91
lines changed

6 files changed

+109
-91
lines changed

dist/bundle.js

Lines changed: 60 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/bundle.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/character.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default class Character {
1818

1919
update(time) {
2020
let footPos = this.state === 'running' ? 11 : 18;
21-
let floorHeight = 117 - this.map.whatsHere(this.pos.x + footPos).height;
21+
let floorHeight = 117 - this.map.heightHere(this.pos.x + footPos);
2222
if (!near(this.pos.y, floorHeight, 0.1) || this.velocity.y < 0 ) {
2323
if (this.velocity.y <= 0) {
2424
this.state = 'jumping';

src/graphics.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,19 @@ Graphics.drawTile = function(tileSet, tileX, tileY, x, y, ctx) {
3939
x, y, tileSet.tileWidth, tileSet.tileHeight);
4040
};
4141

42+
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 .!?,\'":;_-+=/\\[]()#@%{}<>^*&~|→←↑↓○●$€¥'.split('');
4243
Graphics.drawText = function(tileSet, text, x, y, ctx) {
43-
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 .!?,\'":;_-+=/\\[]()#@%{}<>^*&~|→←↑↓○●$€¥'.split('');
44-
var self = this;
4544
if (x === 'center') {
4645
x = Math.floor((ctx.canvas.width - text.length * (tileSet.tileWidth + 1)) / 2);
4746
}
48-
text.split('').forEach(function(char, i) {
49-
let charIndex = chars.indexOf(char);
50-
let charX = x + i * (tileSet.tileWidth + 1);
51-
let charY = ['g', 'j', 'p', 'q', 'y'].indexOf(char) > -1 ? y + 2 : y;
52-
self.drawTile(tileSet, charIndex, 0, charX, charY, ctx);
53-
});
47+
let char, charIndex, charX, charY;
48+
for(let i = 0; i < text.length; i++) {
49+
char = text.charAt(i);
50+
charIndex = chars.indexOf(char);
51+
charX = x + i * (tileSet.tileWidth + 1);
52+
charY = ['g', 'j', 'p', 'q', 'y'].indexOf(char) > -1 ? y + 2 : y;
53+
this.drawTile(tileSet, charIndex, 0, charX, charY, ctx);
54+
};
5455
};
5556

5657
export default Graphics;

src/map.js

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
import { randomBetween } from './utils';
2+
const Y_FROM_FLOOR = [128, 112, 96, 80, 68];
3+
const GROUND = 0;
4+
const COLUMN_ROOT = 1;
5+
const COLUMN_MID = 2;
6+
const COLUMN_TOP = 3;
7+
const CLOUD_RIGHT = 8;
8+
const CLOUD_LEFT = 7;
9+
const PLATFORM = 9;
10+
const HEIGHTS = [-100, 13, 29, 45, 61];
211

312
export default class Map {
413
constructor(tiles, mapstring) {
@@ -13,41 +22,36 @@ export default class Map {
1322
_randomCloud() {
1423
return {x: randomBetween(-6, 160), y: randomBetween(0, 120), speed: randomBetween(0.05, 0.2)};
1524
}
16-
update(time) {
25+
_updateCloud(cloud, time) {
26+
if (cloud.x > -40) {
27+
cloud.x -= time.delta * cloud.speed;
28+
} else {
29+
cloud.x = 160;
30+
cloud.y = randomBetween(0, 120);
31+
cloud.speed = randomBetween(0.05, 0.2);
32+
}
33+
}
34+
update(time, delta, ticks) {
1735
this.x -= time.delta * 0.1;
18-
19-
this.clouds.forEach((cloud) => {
20-
if (cloud.x > -40) {
21-
cloud.x -= time.delta * cloud.speed;
22-
} else {
23-
cloud.x = 160;
24-
cloud.y = this._randomCloud().y;
25-
cloud.speed = this._randomCloud().speed;
26-
}
27-
});
36+
for(let i = 0; i < this.clouds.length; i++) {
37+
this._updateCloud(this.clouds[i], time);
38+
}
2839
}
29-
whatsHere(offset) {
30-
let heights = [-100, 13, 29, 45, 61];
40+
heightHere(offset) {
3141
let index = Math.floor((-this.x + offset) / this.tiles.mapTiles.tileWidth);
3242
let tile = this.stage[index];
33-
return {height: heights[tile]};
43+
return HEIGHTS[tile];
3444
}
3545
render(graphics, ctx) {
36-
const y = [128, 112, 96, 80, 68];
37-
const GROUND = 0;
38-
const COLUMN_ROOT = 1;
39-
const COLUMN_MID = 2;
40-
const COLUMN_TOP = 3;
41-
const PLATFORM = 9;
42-
const CLOUD_LEFT = 7;
43-
const CLOUD_RIGHT = 8;
4446
let leftmostTile = Math.floor((-this.x) / this.tiles.mapTiles.tileWidth);
4547
let rightmostTile = leftmostTile + 10;
4648

47-
this.clouds.forEach((cloud) => {
49+
let cloud;
50+
for(let i = 0; i < this.clouds.length; i++) {
51+
cloud = this.clouds[i];
4852
graphics.drawTile(this.tiles.mapTiles, CLOUD_LEFT, 0, cloud.x, cloud.y, ctx);
4953
graphics.drawTile(this.tiles.mapTiles, CLOUD_RIGHT, 0, cloud.x + 16, cloud.y, ctx);
50-
});
54+
};
5155

5256
this.stage.forEach((t, index) => {
5357
if (index < leftmostTile || index > rightmostTile) {
@@ -61,7 +65,7 @@ export default class Map {
6165
case '2':
6266
case '3':
6367
case '4':
64-
graphics.drawTile(this.tiles.mapTiles, PLATFORM, 0, x, y[t - 1], ctx);
68+
graphics.drawTile(this.tiles.mapTiles, PLATFORM, 0, x, Y_FROM_FLOOR[t - 1], ctx);
6569
break;
6670
}
6771
});

src/timer.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
export default class Timer {
22
constructor(game, timeStep) {
3-
this.runTime = 0;
4-
this.ticks = 0;
3+
this.time = {};
4+
this.time.runTime = 0;
5+
this.time.ticks = 0;
6+
this.time.delta = 0;
57
this.started = false;
68
this.timeStep = timeStep;
79
this.game = game;
@@ -18,12 +20,12 @@ export default class Timer {
1820
let frameTime = newTime - this.currentTime;
1921
this.currentTime = newTime;
2022
while (frameTime > 0) {
21-
var delta = Math.min(frameTime, this.timeStep);
22-
frameTime -= delta;
23-
this.runTime += delta;
24-
this.game.update({delta, ticks: this.ticks, runTime: this.runTime});
23+
this.time.delta = Math.min(frameTime, this.timeStep);
24+
frameTime -= this.time.delta;
25+
this.time.runTime += this.time.delta;
26+
this.game.update(this.time);
2527
}
26-
this.ticks++;
28+
this.time.ticks++;
2729
this.game.render();
2830
}
2931
}

0 commit comments

Comments
 (0)