From a5ffcdc4458cb0a14590dbfad88f068ca2a7cde5 Mon Sep 17 00:00:00 2001 From: Vladimir Agafonkin Date: Wed, 5 Sep 2018 14:56:33 +0300 Subject: [PATCH] clean up, modernize --- README.md | 20 +++++++++----------- bench.js | 10 +++++----- index.js | 4 ++-- package.json | 21 +++++++-------------- rollup.config.js | 4 ++-- test.js | 4 ++-- 6 files changed, 27 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 593f59d..059f458 100644 --- a/README.md +++ b/README.md @@ -66,8 +66,6 @@ Creates a Flatbush index that will hold a given number of items (`numItems`). Ad - `nodeSize`: size of the tree node (`16` by default); experiment with different values for best performance. - `ArrayType`: the array type used for tree storage (`Float64Array` by default); other types may be faster in certain cases (e.g. `Int32Array` when your data is integer). -- `data`: if provided an array or an array buffer from a previously indexed Flatbush object (`index.data` or `index.data.buffer`), -an index will be recreated from this data (useful for transfering indices between threads). #### index.add(minX, minY, maxX, maxY) @@ -106,18 +104,18 @@ Very useful for transfering indices between threads or storing them in a file. ## Performance -Running `npm run bench` with Node v8.10.0: +Running `npm run bench` with Node v10.9.0: ``` 1000000 rectangles -flatbush: 252.849ms -1000 searches 10%: 617.473ms -1000 searches 1%: 66.968ms -1000 searches 0.01%: 7.818ms +flatbush: 256.701ms +1000 searches 10%: 584.421ms +1000 searches 1%: 70.088ms +1000 searches 0.01%: 6.933ms -rbush: 1083.758ms -1000 searches 10%: 920.252ms -1000 searches 1%: 173.104ms -1000 searches 0.01%: 19.057ms +rbush: 1273.758ms +1000 searches 10%: 1207.384ms +1000 searches 1%: 226.523ms +1000 searches 0.01%: 25.242ms ``` diff --git a/bench.js b/bench.js index 4e5bce8..6204fb2 100644 --- a/bench.js +++ b/bench.js @@ -1,11 +1,11 @@ -const Flatbush = require('./index.js').default; -const rbush = require('rbush'); +import Flatbush from './index.js'; +import rbush from 'rbush'; const N = 1000000; const K = 1000; -console.log(N + ' rectangles'); +console.log(`${N} rectangles`); function addRandomBox(arr, boxSize) { const x = Math.random() * (100 - boxSize); @@ -40,7 +40,7 @@ index.finish(); console.timeEnd('flatbush'); function benchSearch(boxes, name) { - const id = K + ' searches ' + name; + const id = `${K} searches ${name}`; console.time(id); for (let i = 0; i < boxes.length; i += 4) { index.search(boxes[i], boxes[i + 1], boxes[i + 2], boxes[i + 3]); @@ -76,7 +76,7 @@ function benchSearchRBush(boxes, name) { maxY: boxes[i + 3] }); } - const id = K + ' searches ' + name; + const id = `${K} searches ${name}`; console.time(id); for (let i = 0; i < boxes2.length; i++) { rbushIndex.search(boxes2[i]); diff --git a/index.js b/index.js index 26c1cd3..25d47e3 100644 --- a/index.js +++ b/index.js @@ -25,12 +25,12 @@ export default class Flatbush { return new Flatbush(numItems, nodeSize, ARRAY_TYPES[versionAndType & 0x0f], data); } - constructor(numItems, nodeSize, ArrayType, data) { + constructor(numItems, nodeSize = 16, ArrayType = Float64Array, data) { if (numItems === undefined) throw new Error('Missing required argument: numItems.'); if (isNaN(numItems) || numItems <= 0) throw new Error(`Unpexpected numItems value: ${numItems}.`); this.numItems = +numItems; - this.nodeSize = Math.min(Math.max(+nodeSize || 16, 2), 65535); + this.nodeSize = Math.min(Math.max(+nodeSize, 2), 65535); // calculate the total number of nodes in the R-tree to allocate space for // and the index of each tree level (used in search later) diff --git a/package.json b/package.json index 68d7c31..0df54c3 100644 --- a/package.json +++ b/package.json @@ -24,14 +24,7 @@ "url": "git+https://github.com/mourner/flatbush.git" }, "eslintConfig": { - "extends": "mourner", - "parserOptions": { - "sourceType": "module" - }, - "rules": { - "no-var": "error", - "prefer-const": "error" - } + "extends": "mourner" }, "keywords": [ "geometry", @@ -48,13 +41,13 @@ }, "homepage": "https://github.com/mourner/flatbush#readme", "devDependencies": { - "eslint": "^4.19.1", - "eslint-config-mourner": "^2.0.3", - "esm": "^3.0.30", + "eslint": "^5.5.0", + "eslint-config-mourner": "^3.0.0", + "esm": "^3.0.82", "rbush": "^2.0.2", - "rollup": "^0.58.2", + "rollup": "^0.65.1", "rollup-plugin-buble": "^0.19.2", - "rollup-plugin-uglify": "^3.0.0", - "tape": "^4.9.0" + "rollup-plugin-terser": "^2.0.2", + "tape": "^4.9.1" } } diff --git a/rollup.config.js b/rollup.config.js index cf28e0d..4ff5749 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,5 +1,5 @@ import buble from 'rollup-plugin-buble'; -import uglify from 'rollup-plugin-uglify' +import {terser} from 'rollup-plugin-terser' const output = (file, plugins) => ({ input: 'index.js', @@ -13,5 +13,5 @@ const output = (file, plugins) => ({ export default [ output('flatbush.js', [buble()]), - output('flatbush.min.js', [uglify(), buble()]) + output('flatbush.min.js', [terser(), buble()]) ]; diff --git a/test.js b/test.js index b736ce1..ed60873 100644 --- a/test.js +++ b/test.js @@ -1,6 +1,6 @@ -const Flatbush = require('./index.js').default; -const test = require('tape').test; +import Flatbush from './index.js'; +import test from 'tape'; const data = [ 8, 62, 11, 66, 57, 17, 57, 19, 76, 26, 79, 29, 36, 56, 38, 56, 92, 77, 96, 80, 87, 70, 90, 74,