Skip to content

Commit

Permalink
feat(find): add option to preCache all features
Browse files Browse the repository at this point in the history
Fixes #78
  • Loading branch information
evansiroky committed Nov 19, 2018
1 parent 0b10cc0 commit 1c0a5b6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The most up-to-date and accurate node.js geographical timezone lookup package.
```javascript
var geoTz = require('geo-tz')

geoTz.preCache() // optionally load all features into memory
geoTz(47.650499, -122.350070) // ['America/Los_Angeles']
geoTz(43.839319, 87.526148) // ['Asia/Shanghai', 'Asia/Urumqi']
```
Expand All @@ -26,7 +27,11 @@ Returns the timezone names found at `lat`, `lon`. The timezone names will be th

This library does an exact geographic lookup which has tradeoffs. It is perhaps a little bit slower that other libraries, has a larger installation size on disk and cannot be used in the browser. However, the results are more accurate than other libraries that compromise by approximating the lookup of the data.

The data is indexed for fast analysis with automatic caching (with time expiration) of subregions of geographic data for when a precise lookup is needed.
The data is indexed for fast analysis with automatic caching with time expiration (or optional an unexpiring cache of the whole world) of subregions of geographic data for when a precise lookup is needed.

### geoTz.preCache()

Loads all geographic features into memory in an unexpiring cache. This has tradeoffs. More memory will be consumed and it will take a little longer before the program is ready to start looking up features, but future lookups will be a lot faster - especially for areas which haven't had a lookup in a while.

## An Important Note About Maintenance

Expand Down
26 changes: 25 additions & 1 deletion lib/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,30 @@ var point = require('@turf/helpers').point

var tzData = require('../data/index.json')

const featureCache = new Cache()
let featureCache = new Cache()

/**
* A function that will load all features into an unexpiring cache
*/
var preCache = function () {
const _eternalCache = {}
featureCache = {
get: (quadPos) => _eternalCache[quadPos]
}

// shoutout to github user @magwo for an initial version of this recursive function
var preloadFeaturesRecursive = function (curTzData, quadPos) {
if (curTzData === 'f') {
var geoJson = loadFeatures(quadPos)
_eternalCache[quadPos] = geoJson
} else if (typeof curTzData === 'object') {
Object.getOwnPropertyNames(curTzData).forEach(function (value) {
preloadFeaturesRecursive(curTzData[value], quadPos + value)
})
}
}
preloadFeaturesRecursive(tzData.lookup, '')
}

var loadFeatures = function (quadPos) {
// exact boundaries saved in file
Expand Down Expand Up @@ -169,3 +192,4 @@ var getTimezone = function (lat, lon) {
}

module.exports = getTimezone
module.exports.preCache = preCache
17 changes: 15 additions & 2 deletions tests/find.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('find tests', function () {
var europeBottomRight = [39.8602076, 34.9127951]
var count = 2000

it('should find timezone of ' + count + ' random european positions', function () {
var findRandomPositions = function () {
var timingStr = 'find tz of ' + count + ' random european positions'
console.time(timingStr)
for (var i = 0; i < count; i++) {
Expand All @@ -61,6 +61,19 @@ describe('find tests', function () {
)
}
console.timeEnd(timingStr)
})
}

it(
'should find timezone of ' + count + ' random european positions with on-demand caching',
findRandomPositions
)

it(
'should find timezone of ' + count + ' random european positions with precache',
function () {
geoTz.preCache()
findRandomPositions()
}
)
})
})

0 comments on commit 1c0a5b6

Please sign in to comment.