Skip to content

Commit 234d547

Browse files
authored
refactor: no need for LRU for interval (2.2m parsed/s) (#683)
1 parent 10b5bc1 commit 234d547

2 files changed

Lines changed: 13 additions & 36 deletions

File tree

packages/graphile-build-pg/src/plugins/PgTypesPlugin.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import type { Plugin } from "graphile-build";
33

44
import makeGraphQLJSONType from "../GraphQLJSON";
55

6-
import { parseInterval as rawParseInterval } from "../postgresInterval";
7-
import LRU from "@graphile/lru";
6+
import { parseInterval } from "../postgresInterval";
87

98
function indent(str) {
109
return " " + str.replace(/\n/g, "\n ");
@@ -14,17 +13,6 @@ function identity(value) {
1413
return value;
1514
}
1615

17-
const parseCache = new LRU({ maxLength: 500 });
18-
function parseInterval(str) {
19-
let result = parseCache.get(str);
20-
if (!result) {
21-
result = rawParseInterval(str);
22-
Object.freeze(result);
23-
parseCache.set(str, result);
24-
}
25-
return result;
26-
}
27-
2816
export default (function PgTypesPlugin(
2917
builder,
3018
{

packages/graphile-build-pg/src/postgresInterval.js

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
// @flow
22

3+
export type Interval = {
4+
years: number,
5+
months: number,
6+
days: number,
7+
hours: number,
8+
minutes: number,
9+
seconds: number,
10+
};
11+
312
// Regexp construction enhanced from `postgres-interval`, which is licensed
413
// under the MIT license and is copyright (c) Ben Drucker <bvdrucker@gmail.com>
514
// (bendrucker.me).
@@ -15,35 +24,15 @@ const DAY = `${NUMBER}\\s+days?`;
1524
const TIME = "([+-])?(\\d+):(\\d\\d):(\\d\\d(?:\\.\\d{1,6})?)";
1625

1726
const INTERVAL = new RegExp(
18-
"^\\s*" +
27+
"^" +
1928
// All parts of an interval are optional
2029
[YEAR, MONTH, DAY, TIME].map(str => "(?:" + str + ")?").join("\\s*") +
21-
"\\s*$"
30+
"$"
2231
);
2332

24-
export type Interval = {
25-
years: number,
26-
months: number,
27-
days: number,
28-
hours: number,
29-
minutes: number,
30-
seconds: number,
31-
};
32-
3333
export function parseInterval(interval: string): Interval {
34-
if (!interval) {
35-
return {
36-
years: 0,
37-
months: 0,
38-
days: 0,
39-
hours: 0,
40-
minutes: 0,
41-
seconds: 0.0,
42-
};
43-
}
44-
4534
const [, years, months, days, plusMinusTime, hours, minutes, seconds] =
46-
INTERVAL.exec(interval) || [];
35+
INTERVAL.exec(interval || "") || [];
4736

4837
const timeMultiplier = plusMinusTime === "-" ? -1 : 1;
4938

0 commit comments

Comments
 (0)