Skip to content

Commit bcab4dd

Browse files
authored
Switch to ESM-only entry point, drop transpiling and IE (#40)
* switch to ESM-only entry point * readme updates * clearer language * try bigger API headings * bring back smaller headings * tiny cleanup * replace broken bundlephobia badge with a static one * adjust badge color * readd rollup bundle for browser CDNs * update cdn links * cleanup
1 parent 0ff1c76 commit bcab4dd

File tree

7 files changed

+43
-71
lines changed

7 files changed

+43
-71
lines changed

.github/workflows/node.yml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
name: Node
2-
3-
on:
4-
push:
5-
branches: ['*']
6-
pull_request:
7-
branches: [master]
8-
2+
on: [push, pull_request]
93
jobs:
104
test:
115
runs-on: ubuntu-latest

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
flatbush.js
2-
flatbush.min.js
32
yarn.lock
43
pnpm-lock.yaml
54
node_modules

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ISC License
22

3-
Copyright (c) 2018, Vladimir Agafonkin
3+
Copyright (c) 2022, Vladimir Agafonkin
44

55
Permission to use, copy, modify, and/or distribute this software for any purpose
66
with or without fee is hereby granted, provided that the above copyright notice

README.md

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Similar to [RBush](https://github.com/mourner/rbush), with the following key dif
1313
Supports geographic locations with the [geoflatbush](https://github.com/mourner/geoflatbush) extension.
1414

1515
[![Build Status](https://github.com/mourner/flatbush/workflows/Node/badge.svg?branch=master)](https://github.com/mourner/flatbush/actions)
16-
[![minzipped size](https://badgen.net/bundlephobia/minzip/flatbush)](https://unpkg.com/flatbush)
16+
[![minzipped size](https://badgen.net/bundlephobia/minzip/flatbush)](https://esm.run/flatbush)
1717
[![Simply Awesome](https://img.shields.io/badge/simply-awesome-brightgreen.svg)](https://github.com/mourner/projects)
1818

1919
## Usage
@@ -46,42 +46,46 @@ const index = Flatbush.from(e.data);
4646

4747
## Install
4848

49-
Install using NPM (`npm install flatbush`) or Yarn (`yarn add flatbush`), then:
49+
Install with NPM: `npm install flatbush`, then import as a module:
5050

5151
```js
52-
// import as an ES module
5352
import Flatbush from 'flatbush';
53+
```
54+
55+
Or use as a module directly in the browser with [jsDelivr](https://www.jsdelivr.com/esm):
5456

55-
// or require as a CommonJS module
56-
const Flatbush = require('flatbush');
57+
```html
58+
<script type="module">
59+
import Flatbush from 'https://cdn.jsdelivr.net/npm/flatbush/+esm';
60+
</script>
5761
```
5862

59-
Or use a browser build directly:
63+
Alternatively, there's a browser bundle with a `Flatbush` global variable:
6064

6165
```html
62-
<script src="https://unpkg.com/flatbush@3.3.1/flatbush.min.js"></script>
66+
<script src="https://cdn.jsdelivr.net/npm/flatbush"></script>
6367
```
6468

6569
## API
6670

67-
#### new Flatbush(numItems[, nodeSize, ArrayType])
71+
#### `new Flatbush(numItems[, nodeSize, ArrayType])`
6872

6973
Creates a Flatbush index that will hold a given number of items (`numItems`). Additionally accepts:
7074

7175
- `nodeSize`: size of the tree node (`16` by default); experiment with different values for best performance (increasing this value makes indexing faster and queries slower, and vise versa).
7276
- `ArrayType`: the array type used for coordinates storage (`Float64Array` by default);
7377
other types may be faster in certain cases (e.g. `Int32Array` when your data is integer).
7478

75-
#### index.add(minX, minY, maxX, maxY)
79+
#### `index.add(minX, minY, maxX, maxY)`
7680

7781
Adds a given rectangle to the index. Returns a zero-based, incremental number that represents the newly added rectangle.
7882

79-
#### index.finish()
83+
#### `index.finish()`
8084

8185
Performs indexing of the added rectangles.
8286
Their number must match the one provided when creating a `Flatbush` object.
8387

84-
#### index.search(minX, minY, maxX, maxY[, filterFn])
88+
#### `index.search(minX, minY, maxX, maxY[, filterFn])`
8589

8690
Returns an array of indices of items intersecting or touching a given bounding box. Item indices refer to the value returned by [`index.add()`](#indexaddminx-miny-maxx-maxy).
8791

@@ -96,7 +100,7 @@ and only includes it if the function returned a truthy value.
96100
const ids = index.search(10, 10, 20, 20, (i) => items[i].foo === 'bar');
97101
```
98102

99-
#### index.neighbors(x, y[, maxResults, maxDistance, filterFn])
103+
#### `index.neighbors(x, y[, maxResults, maxDistance, filterFn])`
100104

101105
Returns an array of item indices in order of distance from the given `x, y`
102106
(known as K nearest neighbors, or KNN). Item indices refer to the value returned by [`index.add()`](#indexaddminx-miny-maxx-maxy).
@@ -108,13 +112,13 @@ const ids = index.neighbors(10, 10, 5); // returns 5 ids
108112
`maxResults` and `maxDistance` are `Infinity` by default.
109113
Also accepts a `filterFn` similar to `index.search`.
110114

111-
#### Flatbush.from(data)
115+
#### `Flatbush.from(data)`
112116

113117
Recreates a Flatbush index from raw `ArrayBuffer` data
114118
(that's exposed as `index.data` on a previously indexed Flatbush instance).
115119
Very useful for transferring indices between threads or storing them in a file.
116120

117-
#### Properties
121+
### Properties
118122

119123
- `data`: array buffer that holds the index.
120124
- `minX`, `minY`, `maxX`, `maxY`: bounding box of the data.
@@ -125,14 +129,14 @@ Very useful for transferring indices between threads or storing them in a file.
125129

126130
## Performance
127131

128-
Running `npm run bench` with Node v10.11.0:
132+
Running `node bench.js` with Node v14:
129133

130134
bench | flatbush | rbush
131135
--- | --- | ---
132-
index 1,000,000 rectangles | 263ms | 1208ms
133-
1000 searches 10% | 594ms | 1105ms
134-
1000 searches 1% | 68ms | 213ms
135-
1000 searches 0.01% | 9ms | 27ms
136-
1000 searches of 100 neighbors | 29ms | 58ms
137-
1 search of 1,000,000 neighbors | 148ms | 781ms
138-
100,000 searches of 1 neighbor | 870ms | 1548ms
136+
index 1,000,000 rectangles | 273ms | 1143ms
137+
1000 searches 10% | 575ms | 781ms
138+
1000 searches 1% | 63ms | 155ms
139+
1000 searches 0.01% | 6ms | 17ms
140+
1000 searches of 100 neighbors | 24ms | 43ms
141+
1 search of 1,000,000 neighbors | 133ms | 280ms
142+
100,000 searches of 1 neighbor | 710ms | 1170ms

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,14 @@ export default class Flatbush {
180180

181181
// search through child nodes
182182
for (let pos = nodeIndex; pos < end; pos += 4) {
183-
const index = this._indices[pos >> 2] | 0;
184-
185183
// check if node bbox intersects with query bbox
186184
if (maxX < this._boxes[pos]) continue; // maxX < nodeMinX
187185
if (maxY < this._boxes[pos + 1]) continue; // maxY < nodeMinY
188186
if (minX > this._boxes[pos + 2]) continue; // minX > nodeMaxX
189187
if (minY > this._boxes[pos + 3]) continue; // minY > nodeMaxY
190188

189+
const index = this._indices[pos >> 2] | 0;
190+
191191
if (nodeIndex < this.numItems * 4) {
192192
if (filterFn === undefined || filterFn(index)) {
193193
results.push(index); // leaf item

package.json

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22
"name": "flatbush",
33
"version": "3.3.1",
44
"description": "Fast static spatial index for rectangles",
5+
"author": "Vladimir Agafonkin",
6+
"license": "ISC",
7+
"type": "module",
58
"main": "flatbush.js",
69
"module": "index.js",
7-
"unpkg": "flatbush.min.js",
8-
"jsdelivr": "flatbush.min.js",
10+
"exports": "./index.js",
11+
"sideEffects": false,
912
"scripts": {
1013
"pretest": "eslint index.js test.js bench.js",
11-
"test": "node -r esm test.js",
12-
"bench": "node -r esm bench.js",
13-
"build": "rollup -c",
14+
"test": "node test.js",
15+
"build": "rollup index.js -o flatbush.js -n Flatbush -f umd -p node-resolve",
1416
"prepublishOnly": "npm run build"
1517
},
1618
"files": [
1719
"index.js",
18-
"flatbush.js",
19-
"flatbush.min.js"
20+
"flatbush.js"
2021
],
21-
"@std/esm": "js",
2222
"repository": {
2323
"type": "git",
2424
"url": "git+https://github.com/mourner/flatbush.git"
@@ -34,25 +34,19 @@
3434
"rectangle",
3535
"search"
3636
],
37-
"author": "Vladimir Agafonkin",
38-
"license": "ISC",
39-
"bugs": {
40-
"url": "https://github.com/mourner/flatbush/issues"
37+
"engines": {
38+
"node": ">= 12.17.0"
39+
},
40+
"dependencies": {
41+
"flatqueue": "^2.0.3"
4142
},
42-
"homepage": "https://github.com/mourner/flatbush#readme",
4343
"devDependencies": {
44-
"@rollup/plugin-buble": "^0.21.3",
4544
"@rollup/plugin-node-resolve": "^13.1.3",
4645
"eslint": "^8.12.0",
4746
"eslint-config-mourner": "^3.0.0",
48-
"esm": "^3.2.25",
4947
"rbush": "^3.0.1",
5048
"rbush-knn": "^3.0.1",
5149
"rollup": "^2.70.1",
52-
"rollup-plugin-terser": "^7.0.2",
5350
"tape": "^5.5.2"
54-
},
55-
"dependencies": {
56-
"flatqueue": "^1.2.1"
5751
}
5852
}

rollup.config.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)