From 803e8a94f86a13eee33a1ca876f8d5bc81d3196a Mon Sep 17 00:00:00 2001 From: Mariano Guerra Date: Thu, 28 Oct 2021 10:34:25 +0100 Subject: [PATCH] reverse empty argument check in constructors (#1887) The most common way to call a constructor to make an empty collection is to call it without arguments, then value is undefined. With this commit the first check to see if the value passed to the constructor is not set is for undefined, the most common way to call it. This way the condition will only have to evaluate the left side for the common case. --- src/List.js | 2 +- src/Map.js | 2 +- src/OrderedMap.js | 2 +- src/OrderedSet.js | 2 +- src/Seq.js | 6 +++--- src/Set.js | 2 +- src/Stack.js | 2 +- 7 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/List.js b/src/List.js index 855b47682..5f3784673 100644 --- a/src/List.js +++ b/src/List.js @@ -31,7 +31,7 @@ export class List extends IndexedCollection { constructor(value) { const empty = emptyList(); - if (value === null || value === undefined) { + if (value === undefined || value === null) { return empty; } if (isList(value)) { diff --git a/src/Map.js b/src/Map.js index 9ff650c9b..9a3628c69 100644 --- a/src/Map.js +++ b/src/Map.js @@ -36,7 +36,7 @@ export class Map extends KeyedCollection { // @pragma Construction constructor(value) { - return value === null || value === undefined + return value === undefined || value === null ? emptyMap() : isMap(value) && !isOrdered(value) ? value diff --git a/src/OrderedMap.js b/src/OrderedMap.js index 7ed03e569..6da2d762b 100644 --- a/src/OrderedMap.js +++ b/src/OrderedMap.js @@ -10,7 +10,7 @@ export class OrderedMap extends Map { // @pragma Construction constructor(value) { - return value === null || value === undefined + return value === undefined || value === null ? emptyOrderedMap() : isOrderedMap(value) ? value diff --git a/src/OrderedSet.js b/src/OrderedSet.js index b1b8140b4..c7b351ba0 100644 --- a/src/OrderedSet.js +++ b/src/OrderedSet.js @@ -10,7 +10,7 @@ export class OrderedSet extends Set { // @pragma Construction constructor(value) { - return value === null || value === undefined + return value === undefined || value === null ? emptyOrderedSet() : isOrderedSet(value) ? value diff --git a/src/Seq.js b/src/Seq.js index d9cdbd871..bf467c5ff 100644 --- a/src/Seq.js +++ b/src/Seq.js @@ -23,7 +23,7 @@ import isArrayLike from './utils/isArrayLike'; export class Seq extends Collection { constructor(value) { - return value === null || value === undefined + return value === undefined || value === null ? emptySequence() : isImmutable(value) ? value.toSeq() @@ -85,7 +85,7 @@ export class Seq extends Collection { export class KeyedSeq extends Seq { constructor(value) { - return value === null || value === undefined + return value === undefined || value === null ? emptySequence().toKeyedSeq() : isCollection(value) ? isKeyed(value) @@ -103,7 +103,7 @@ export class KeyedSeq extends Seq { export class IndexedSeq extends Seq { constructor(value) { - return value === null || value === undefined + return value === undefined || value === null ? emptySequence() : isCollection(value) ? isKeyed(value) diff --git a/src/Set.js b/src/Set.js index a910760cf..067a1f7d7 100644 --- a/src/Set.js +++ b/src/Set.js @@ -15,7 +15,7 @@ export class Set extends SetCollection { // @pragma Construction constructor(value) { - return value === null || value === undefined + return value === undefined || value === null ? emptySet() : isSet(value) && !isOrdered(value) ? value diff --git a/src/Stack.js b/src/Stack.js index 0a5ba8a65..6a7a6f219 100644 --- a/src/Stack.js +++ b/src/Stack.js @@ -13,7 +13,7 @@ export class Stack extends IndexedCollection { // @pragma Construction constructor(value) { - return value === null || value === undefined + return value === undefined || value === null ? emptyStack() : isStack(value) ? value