Skip to content

Commit

Permalink
reverse empty argument check in constructors (#1887)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
marianoguerra committed Oct 28, 2021
1 parent a5e5af4 commit 803e8a9
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/List.js
Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Map.js
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/OrderedMap.js
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/OrderedSet.js
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/Seq.js
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/Set.js
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Stack.js
Expand Up @@ -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
Expand Down

0 comments on commit 803e8a9

Please sign in to comment.