Делая игру на node js столкнулся с долгой обработкой объектов. В массиве свыше 700 объектов, как можно проверять каждый, не тормозя сервер? То-есть чем можно заменить этот кусок кода, чтобы не было зависаний, если эта функция выполняется каждые 13 миллисекунд?
GIF: https://imgur.com/a/bCJ0z1e (Синяя полоска это направление игрока, должен следовать за курсором.)
GIF (10 объектов на карте): https://imgur.com/a/01CvGYe (Зависания только из за записи)
function update() {
for (var name in objects) {
objectC = objects[name];
if (objectC.canmove == 1) {
if (objectC.mY == 0 && objectC.mX == 0) {
objectC.mY = Math.floor(Math.random() * 11) -5;
objectC.mX = Math.floor(Math.random() * 11) -5;
}
if (objectC.speed == 0) {
objectC.speed = Math.random();
}
speedObj = objectC.speed || config.movspeed / 3;
var objAngle = Math.atan2(objectC.mY, objectC.mX);
var objDist = Math.sqrt(sq(objectC.mX) + sq(objectC.mY));
objectC.anglez = angle(0, 0, objectC.mX, objectC.mY);
speed = Math.min(speedObj, 0.035 * Math.pow(objDist, impImitz));
if (speed < 0.06 && speed > -0.06) { speed = 0 }
objectC.vx = speed * Math.cos(objAngle);
objectC.vy = speed * Math.sin(objAngle);
objectC.x += objectC.vx;
objectC.y += objectC.vy;
}
if (objectC.x <= config.leftBorder || objectC.x-objectC.size <= config.leftBorder) {
objectC.x = config.rightBorder - objectC.size - 25;
} else if (objectC.x >= config.rightBorder || objectC.x+objectC.size >= config.rightBorder - 25) {
objectC.x = config.leftBorder + objectC.size + 25;
}
if (objectC.y <= config.upBorder || objectC.y-objectC.size <= config.upBorder) {
objectC.y = config.downBorder - objectC.size - 25;
} else if (objectC.y >= config.downBorder || objectC.y+objectC.size >= config.downBorder - 25) {
objectC.y = config.upBorder + objectC.size + 25;
}
if (objectC.angle >= 360) {
objectC.angle = 0;
}
objectC.angle += objectC.speedZ;
objectC.size = objectC.mass / 2;
}
for (var name in players) {
var playerCheck = players[name];
var playerCheckMouse = gameMouse[name];
if (playerCheck.spectator == 1) {
playerCheck.x += playerCheck.spectatorspeedx;
playerCheck.y += playerCheck.spectatorspeedy;
playerCheck.name = "";
if (playerCheck.x <= config.leftBorder) {
playerCheck.spectatorspeedx = 3;
} else if (playerCheck.x >= config.rightBorder) {
playerCheck.spectatorspeedx = -3;
}
if (playerCheck.y <= config.upBorder) {
playerCheck.spectatorspeedy = 3;
} else if (playerCheck.y >= config.downBorder) {
playerCheck.spectatorspeedy = -3;
}
}
if (playerCheck.spectator == 0) {
if (playerCheck.hp <= 0) {
playerCheck.spectator = 1;
playerCheck.hp = 100;
playerCheck.h = 100;
playerCheck.mass = playerCheck.mass / 2;
setSysmsg("You're dead! Watch your health!", playerCheck.clientid);
} else {
if (playerCheck.abilitycooldown > 0) {
playerCheck.abilitycooldown -= 1;
}
playerCheck.mX = playerCheckMouse.x;
playerCheck.mY = playerCheckMouse.y;
speedPlr = playerCheck.speed || config.movspeed;
var mouseAngle = Math.atan2(playerCheck.mY, playerCheck.mX);
var mouseDist = Math.sqrt(sq(playerCheck.mX) + sq(playerCheck.mY));
playerCheck.angle = angle(0, 0, playerCheck.mX, playerCheck.mY);
speed = Math.min(speedPlr, 0.035 * Math.pow(mouseDist, impImitz));
if (speed < 0.06 && speed > -0.06) { speed = 0 }
playerCheck.vx = speed * Math.cos(mouseAngle);
playerCheck.vy = speed * Math.sin(mouseAngle);
playerCheck.x += playerCheck.vx;
playerCheck.y += playerCheck.vy;
if (playerCheck.x <= config.leftBorder || playerCheck.x-playerCheck.size <= config.leftBorder) {
playerCheck.x = config.rightBorder - playerCheck.size - 25;
} else if (playerCheck.x >= config.rightBorder || playerCheck.x+playerCheck.size >= config.rightBorder - 25) {
playerCheck.x = config.leftBorder + playerCheck.size + 25;
}
if (playerCheck.y <= config.upBorder || playerCheck.y-playerCheck.size <= config.upBorder) {
playerCheck.y = config.downBorder - playerCheck.size - 25;
} else if (playerCheck.y >= config.downBorder || playerCheck.y+playerCheck.size >= config.downBorder - 25) {
playerCheck.y = config.upBorder + playerCheck.size + 25;
}
for (var objid in objects) {
objectZ = objects[objid];
if (playerCheck.y+(playerCheck.size-5) + 2 * Math.PI > objectZ.y && playerCheck.y-(playerCheck.size-5) - 2 * Math.PI < objectZ.y && playerCheck.x+(playerCheck.size-5) + 2 * Math.PI > objectZ.x && playerCheck.x-(playerCheck.size-5) - 2 * Math.PI < objectZ.x) {
if (objectZ.type == "asteroid") {
foodCount--;
playerCheck.mass += Number(objectZ.mass / 2);
playerCheck.h += Number(objectZ.mass / 2);
delete objects[objid];
}
}
}
if (playerCheck.mass > playerCheck.xpToUpgrade) {
playerCheck.size = playerCheck.xpToUpgrade / 5;
} else {
playerCheck.size = playerCheck.mass / 5;
}
if (playerCheck.size < 25) {
playerCheck.size = 15;
}
playerCheck.camzoom = 1;
}
}
}
}
setInterval(update, 13);
node --prof-process
Statistical profiling result from isolate-v8.log, (16475 ticks, 3 unaccounted, 0 excluded).
[Shared libraries]:
ticks total nonlib name
13297 80.7% ..\SYSTEM32\ntdll.dll
3087 18.7% ..\nodejs\node.exe
9 0.1% ..\system32\KERNELBASE.dll
6 0.0% ..\system32\KERNEL32.DLL
1 0.0% ..\system32\WS2_32.dll
[JavaScript]:
ticks total nonlib name
36 0.2% 48.0% LazyCompile: *update ..\index.js:35:20
10 0.1% 13.3% LazyCompile: *processTimers internal/timers.js:477:25
6 0.0% 8.0% LazyCompile: *listOnTimeout internal/timers.js:497:25
5 0.0% 6.7% LazyCompile: *Writable.write _stream_writable.js:289:36
3 0.0% 4.0% LazyCompile: *validateAsyncId internal/async_hooks.js:120:25
2 0.0% 2.7% LazyCompile: *draw ..\index.js:498:17
2 0.0% 2.7% LazyCompile: *_copy buffer.js:192:15
2 0.0% 2.7% LazyCompile: *WebSocketFrame.toBuffer ..\node_modules\websocket\lib\WebSocketFrame.js:174:45
1 0.0% 1.3% LazyCompile: *toInteger buffer.js:182:19
1 0.0% 1.3% LazyCompile: *resolve path.js:130:10
1 0.0% 1.3% LazyCompile: *pushAsyncIds internal/async_hooks.js:404:22
1 0.0% 1.3% LazyCompile: *percolateDown internal/priority_queue.js:49:16
1 0.0% 1.3% LazyCompile: *normalizeString path.js:52:25
1 0.0% 1.3% LazyCompile: *debug ..\node_modules\debug\src\debug.js:65:17
[C++]:
ticks total nonlib name
[Summary]:
ticks total nonlib name
72 0.4% 96.0% JavaScript
0 0.0% 0.0% C++
45 0.3% 60.0% GC
16400 99.5% Shared libraries
3 0.0% Unaccounted
[C++ entry points]:
ticks cpp total name
[Bottom up (heavy) profile]:
Note: percentage shows a share of a particular caller in the total
amount of its parent calls.
Callers occupying less than 1.0% are not shown.
ticks parent name
13297 80.7% ..\SYSTEM32\ntdll.dll
3087 18.7% ..\nodejs\node.exe
2474 80.1% ..\nodejs\node.exe
2063 83.4% LazyCompile: *draw ..\index.js:498:17
1392 67.5% LazyCompile: *processTimers internal/timers.js:477:25
671 32.5% LazyCompile: *listOnTimeout internal/timers.js:497:25
671 100.0% LazyCompile: *processTimers internal/timers.js:477:25
78 3.2% LazyCompile: ~draw ..\index.js:498:17
50 2.0% LazyCompile: *update ..\index.js:35:20
26 52.0% LazyCompile: *processTimers internal/timers.js:477:25
12 24.0% LazyCompile: ~listOnTimeout internal/timers.js:497:25
8 66.7% LazyCompile: ~processTimers internal/timers.js:477:25
4 33.3% LazyCompile: *processTimers internal/timers.js:477:25
12 24.0% LazyCompile: *listOnTimeout internal/timers.js:497:25
12 100.0% LazyCompile: *processTimers internal/timers.js:477:25
45 1.8% LazyCompile: *_copy buffer.js:192:15
45 100.0% LazyCompile: *WebSocketFrame.toBuffer ..\node_modules\websocket\lib\WebSocketFrame.js:174:45
45 100.0% LazyCompile: *draw ..\index.js:498:17
30 66.7% LazyCompile: *processTimers internal/timers.js:477:25
15 33.3% LazyCompile: *listOnTimeout internal/timers.js:497:25
27 1.1% LazyCompile: ~compileForInternalLoader internal/bootstrap/loaders.js:266:27
25 92.6% LazyCompile: ~nativeModuleRequire internal/bootstrap/loaders.js:299:29
2 8.0% LazyCompile: ~initializeCJSLoader internal/bootstrap/pre_execution.js:395:29
2 100.0% LazyCompile: ~prepareMainThreadExecution internal/bootstrap/pre_execution.js:14:36
2 8.0% Eval: ~<anonymous> internal/bootstrap/node.js:1:1
2 8.0% Eval: ~<anonymous> http.js:1:1
2 100.0% LazyCompile: ~compileForInternalLoader internal/bootstrap/loaders.js:266:27
1 4.0% LazyCompile: ~setupPrepareStackTrace internal/bootstrap/node.js:251:32
1 100.0% Eval: ~<anonymous> internal/bootstrap/node.js:1:1
1 4.0% LazyCompile: ~setupBuffer internal/bootstrap/node.js:329:21
1 100.0% Eval: ~<anonymous> internal/bootstrap/node.js:1:1
1 4.0% Eval: ~<anonymous> tty.js:1:1
1 100.0% LazyCompile: ~compileForInternalLoader internal/bootstrap/loaders.js:266:27
1 4.0% Eval: ~<anonymous> tls.js:1:1
1 100.0% LazyCompile: ~compileForInternalLoader internal/bootstrap/loaders.js:266:27
1 4.0% Eval: ~<anonymous> stream.js:1:1
1 100.0% LazyCompile: ~compileForInternalLoader internal/bootstrap/loaders.js:266:27
1 4.0% Eval: ~<anonymous> internal/util/inspect.js:1:1
1 100.0% LazyCompile: ~compileForInternalLoader internal/bootstrap/loaders.js:266:27
1 4.0% Eval: ~<anonymous> internal/url.js:1:1
1 100.0% LazyCompile: ~compileForInternalLoader internal/bootstrap/loaders.js:266:27
1 4.0% Eval: ~<anonymous> internal/timers.js:1:1
1 100.0% LazyCompile: ~compileForInternalLoader internal/bootstrap/loaders.js:266:27
1 4.0% Eval: ~<anonymous> internal/source_map/source_map_cache.js:1:1
1 100.0% LazyCompile: ~compileForInternalLoader internal/bootstrap/loaders.js:266:27
1 4.0% Eval: ~<anonymous> internal/modules/cjs/helpers.js:1:1
1 100.0% LazyCompile: ~compileForInternalLoader internal/bootstrap/loaders.js:266:27
1 4.0% Eval: ~<anonymous> internal/cluster/master.js:1:1
1 100.0% LazyCompile: ~compileForInternalLoader internal/bootstrap/loaders.js:266:27
1 4.0% Eval: ~<anonymous> internal/bootstrap/pre_execution.js:1:1
1 100.0% LazyCompile: ~compileForInternalLoader internal/bootstrap/loaders.js:266:27
1 4.0% Eval: ~<anonymous> fs.js:1:1
1 100.0% LazyCompile: ~compileForInternalLoader internal/bootstrap/loaders.js:266:27
1 4.0% Eval: ~<anonymous> events.js:1:1
1 100.0% LazyCompile: ~compileForInternalLoader internal/bootstrap/loaders.js:266:27
1 4.0% Eval: ~<anonymous> crypto.js:1:1
1 100.0% LazyCompile: ~compileForInternalLoader internal/bootstrap/loaders.js:266:27
1 4.0% Eval: ~<anonymous> cluster.js:1:1
1 100.0% LazyCompile: ~compileForInternalLoader internal/bootstrap/loaders.js:266:27
1 4.0% Eval: ~<anonymous> buffer.js:1:1
1 100.0% LazyCompile: ~compileForInternalLoader internal/bootstrap/loaders.js:266:27
1 4.0% Eval: ~<anonymous> _stream_readable.js:1:1
1 100.0% LazyCompile: ~compileForInternalLoader internal/bootstrap/loaders.js:266:27
1 4.0% Eval: ~<anonymous> _http_common.js:1:1
1 100.0% LazyCompile: ~compileForInternalLoader internal/bootstrap/loaders.js:266:27
2 7.4% LazyCompile: ~compileForPublicLoader internal/bootstrap/loaders.js:213:25
2 100.0% LazyCompile: ~loadNativeModule internal/modules/cjs/helpers.js:22:26
2 100.0% LazyCompile: ~Module._load internal/modules/cjs/loader.js:845:24
39 1.3% LazyCompile: *processTimers internal/timers.js:477:25
33 1.1% LazyCompile: *update ..\index.js:35:20
21 63.6% LazyCompile: *processTimers internal/timers.js:477:25
7 21.2% LazyCompile: ~listOnTimeout internal/timers.js:497:25
6 85.7% LazyCompile: *processTimers internal/timers.js:477:25
1 14.3% LazyCompile: ~processTimers internal/timers.js:477:25
5 15.2% LazyCompile: *listOnTimeout internal/timers.js:497:25
5 100.0% LazyCompile: *processTimers internal/timers.js:477:25