Skip to content
Permalink
Browse files

speed up parsing even more!

optimized by writing a custom parser that works on the raw
ArrayBuffer instead of costly string operations.

down to almost 1/3rd of the original time (parsing alone is 85%
faster now): 17 seconds instead of 46 in my previous example.

Now, building the rtree is by far the slowest part (~11 seconds)
  • Loading branch information...
tyrasd committed Sep 2, 2016
1 parent 5ff3bce commit e0f95624e882e7fb30a466c784dfa8b53046e659
Showing with 29 additions and 16 deletions.
  1. +3 −4 index.html
  2. +26 −12 index.js
@@ -115,13 +115,12 @@
reader.onload = function (event) {
processing.className = 'done';
legend.className = 'done';
console.timeEnd("read file")
console.timeEnd("read file");
L.tileLayer.osmTileAccessLogLayer(event.target.result).addTo(map);
};
console.log(file);
console.time("read file")
reader.readAsText(file);
console.time("read file");
reader.readAsArrayBuffer(file);
return false;
};
@@ -4,20 +4,34 @@ L.TileLayer.OsmTileAccessLogLayer = L.TileLayer.Canvas.extend({
maxZoom:19-8,
data: null
},
initialize: function (data) {
initialize: function (arrayBuffer) {
console.time("parse data")
data = data.split("\n")
data.pop() // newline at the end
data = data.map(function(line) {
line = line.split(' ')
var coords = line[0].split('/')
return {
x: +coords[1],
y: +coords[2],
zoom: +coords[0],
count: +line[1]
var view = new Uint8Array(arrayBuffer)
var data = []
var currentInt = 0
var currentCoords = []
for (var i = 0; i<view.length; i++) {
switch (view[i]) {
default:
currentInt = currentInt*10 + (view[i] - 48 /*'0'*/)
break;
case 10: // '\n'
data.push({
x: currentCoords[1],
y: currentCoords[2],
zoom: currentCoords[0],
count: currentInt
})
currentCoords = []
currentInt = 0
break;
case 32: // ' '
case 47: // '/'
currentCoords.push(currentInt)
currentInt = 0
break;
}
})
}
console.timeEnd("parse data")
console.time("build indices")
var tree = rbush(9, ['.x', '.y', '.x', '.y'])

0 comments on commit e0f9562

Please sign in to comment.
You can’t perform that action at this time.