-
-
Save anonymous/456a601e126d30d61e06d4baf2f6bafa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//var _ = require ('lodash'); | |
var Benchmark = require('benchmark'); | |
suite = new Benchmark.Suite; | |
// Populate the base array | |
var typeRegister = {}; | |
var type = "creep"; | |
for (var i = 0; i < 50; i++) { | |
typeRegister["" + Math.floor(Math.random() * 10000)] = {pos: {x: 5 + Math.random() * 10, y: Math.random() * 20, roomName: "E0N0"}, type: type}; | |
} | |
var id = "E0N0"; | |
var top = 0; | |
var bottom = 12; | |
var left = 3; | |
var right = 14; | |
var privateStore = {}; | |
privateStore[id] = { lookTypeRegisters: {} }; | |
privateStore[id].lookTypeRegisters[type] = typeRegister; | |
Benchmark.options.async = true; | |
Benchmark.options.delay = 0.05; | |
Benchmark.options.minSamples = 200; | |
function _lookAreaMixedRegister_FOR(id, type, top, left, bottom, right, withType, asArray, result) { | |
var typeRegister = privateStore[id].lookTypeRegisters[type], | |
keys = typeRegister && Object.keys(typeRegister); | |
if(type != 'terrain' && keys.length < (bottom-top+1)*(right-left+1)) { | |
// by objects | |
var checkInside = (i) => { | |
return (!i.pos && i.roomName == id || i.pos && i.pos.roomName == id) && | |
i.pos && i.pos.y >= top && i.pos.y <= bottom && i.pos.x >= left && i.pos.x <= right || | |
!i.pos && i.y >= top && i.y <= bottom && i.x >= left && i.x <= right; | |
}; | |
var item; | |
for (var i = 0, len = keys.length; i < len; i++) { | |
var obj = typeRegister[keys[i]]; | |
if(checkInside(obj)) { | |
if(withType) { | |
item = {type: type}; | |
item[type] = obj; | |
if(asArray) { | |
result.push({x: obj.x || obj.pos.x, y: obj.y || obj.pos.y, type, [type]: obj}); | |
} | |
else { | |
result[obj.y || obj.pos.y][obj.x || obj.pos.x].push(item); | |
} | |
} | |
else { | |
if(asArray) { | |
result.push({x: obj.x || obj.pos.x, y: obj.y || obj.pos.y, [type]: obj}); | |
} | |
else { | |
result[obj.y || obj.pos.y][obj.x || obj.pos.x] = result[obj.y || obj.pos.y][obj.x || obj.pos.x] || []; | |
result[obj.y || obj.pos.y][obj.x || obj.pos.x].push(obj); | |
} | |
} | |
} | |
} | |
} | |
else { | |
// spatial | |
for (var y = top; y <= bottom; y++) { | |
for (var x = left; x <= right; x++) { | |
if(asArray) { | |
_lookSpatialRegister(id, type, x, y, result, true); | |
} | |
else { | |
if (result[y][x]) { | |
_lookSpatialRegister(id, type, x, y, result[y][x]); | |
} | |
else { | |
result[y][x] = _lookSpatialRegister(id, type, x, y, undefined); | |
} | |
} | |
} | |
} | |
} | |
} | |
function _lookAreaMixedRegister_FOREACH(id, type, top, left, bottom, right, withType, asArray, result) { | |
var typeRegister = privateStore[id].lookTypeRegisters[type], | |
keys = typeRegister && Object.keys(typeRegister); | |
if(type != 'terrain' && keys.length < (bottom-top+1)*(right-left+1)) { | |
// by objects | |
var checkInside = (i) => { | |
return (!i.pos && i.roomName == id || i.pos && i.pos.roomName == id) && | |
i.pos && i.pos.y >= top && i.pos.y <= bottom && i.pos.x >= left && i.pos.x <= right || | |
!i.pos && i.y >= top && i.y <= bottom && i.x >= left && i.x <= right; | |
}; | |
var item; | |
keys.forEach((key) => { | |
var obj = typeRegister[key]; | |
if(checkInside(obj)) { | |
if(withType) { | |
item = {type: type}; | |
item[type] = obj; | |
if(asArray) { | |
result.push({x: obj.x || obj.pos.x, y: obj.y || obj.pos.y, type, [type]: obj}); | |
} | |
else { | |
result[obj.y || obj.pos.y][obj.x || obj.pos.x].push(item); | |
} | |
} | |
else { | |
if(asArray) { | |
result.push({x: obj.x || obj.pos.x, y: obj.y || obj.pos.y, [type]: obj}); | |
} | |
else { | |
result[obj.y || obj.pos.y][obj.x || obj.pos.x] = result[obj.y || obj.pos.y][obj.x || obj.pos.x] || []; | |
result[obj.y || obj.pos.y][obj.x || obj.pos.x].push(obj); | |
} | |
} | |
} | |
}); | |
} | |
else { | |
// spatial | |
for (var y = top; y <= bottom; y++) { | |
for (var x = left; x <= right; x++) { | |
if(asArray) { | |
_lookSpatialRegister(id, type, x, y, result, true); | |
} | |
else { | |
if (result[y][x]) { | |
_lookSpatialRegister(id, type, x, y, result[y][x]); | |
} | |
else { | |
result[y][x] = _lookSpatialRegister(id, type, x, y, undefined); | |
} | |
} | |
} | |
} | |
} | |
} | |
// add tests | |
suite | |
.add('forEach (Current)', function() { | |
var result = []; | |
_lookAreaMixedRegister_FOREACH(id, type, top, left, bottom, right, true, true, result); | |
}) | |
.add('for', function() { | |
var result = []; | |
_lookAreaMixedRegister_FOR(id, type, top, left, bottom, right, true, true, result); | |
}) | |
// add listeners | |
.on('cycle', function(event) { | |
console.log(String(event.target)); | |
}) | |
.on('complete', function() { | |
console.log('Fastest is ' + this.filter('fastest').map('name')); | |
}) | |
.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment