From 00761f9aa29c795a5a72a92b657ddb117a5c2a94 Mon Sep 17 00:00:00 2001 From: Vincent Driessen Date: Tue, 10 Oct 2017 12:37:56 +0200 Subject: [PATCH] Let all paramless decoder factories return singletons By returning singleton instances of their decoders, this becomes slightly faster, takes up less memory, and makes it possible to compare decoders for identity, should you want that... decodeNumber() === decodeNumber() --- src/boolean.js | 10 ++++++---- src/constants.js | 13 ++++++++----- src/number.js | 10 ++++++---- src/string.js | 10 ++++++---- 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/boolean.js b/src/boolean.js index 7d86b345..ab21f771 100644 --- a/src/boolean.js +++ b/src/boolean.js @@ -3,13 +3,15 @@ import { assertType } from './asserts'; import type { Decoder } from './types'; +const booleanDecoder: Decoder = blob => { + assertType(blob, 'boolean'); + return (blob: boolean); +}; + /** * Decodes a boolean value. * Will throw a DecodeError if anything other than a boolean value is found. */ export function decodeBoolean(): Decoder { - return (blob: any) => { - assertType(blob, 'boolean'); - return (blob: boolean); - }; + return booleanDecoder; } diff --git a/src/constants.js b/src/constants.js index db7de578..78e42296 100644 --- a/src/constants.js +++ b/src/constants.js @@ -3,6 +3,12 @@ import { assertTest, assertType } from './asserts'; import type { Decoder } from './types'; +const nullDecoder: Decoder = decodeConstant(null); +const undefinedDecoder: Decoder = blob => { + assertType(blob, 'undefined'); + return undefined; +}; + /** * Decodes any hardcoded value, without looking at the input data. */ @@ -31,15 +37,12 @@ export function decodeConstant(value: T): Decoder { * Decodes the null value. */ export function decodeNull(): Decoder { - return decodeConstant(null); + return nullDecoder; } /** * Decodes the undefined value. */ export function decodeUndefined(): Decoder { - return (blob: any) => { - assertType(blob, 'undefined'); - return undefined; - }; + return undefinedDecoder; } diff --git a/src/number.js b/src/number.js index 1c32a844..9a8201c7 100644 --- a/src/number.js +++ b/src/number.js @@ -3,6 +3,11 @@ import { assertTest } from './asserts'; import type { Decoder } from './types'; +const numberDecoder: Decoder = blob => { + assertTest(blob, Number.isFinite, 'Not a number', 'Expected a finite number'); + return (blob: number); +}; + /** * Decodes a finite (!) number (integer or float) value. Will throw * a `DecodeError` if anything other than a finite number value is found. This @@ -10,8 +15,5 @@ import type { Decoder } from './types'; * considered valid numbers. */ export function decodeNumber(): Decoder { - return (blob: any) => { - assertTest(blob, Number.isFinite, 'Not a number', 'Expected a finite number'); - return (blob: number); - }; + return numberDecoder; } diff --git a/src/string.js b/src/string.js index 2195c53f..3e45b4c1 100644 --- a/src/string.js +++ b/src/string.js @@ -3,13 +3,15 @@ import { assertType } from './asserts'; import type { Decoder } from './types'; +const stringDecoder: Decoder = blob => { + assertType(blob, 'string'); + return (blob: string); +}; + /** * Decodes a string value. * Will throw a DecodeError if anything other than a string value is found. */ export function decodeString(): Decoder { - return (blob: any) => { - assertType(blob, 'string'); - return (blob: string); - }; + return stringDecoder; }