View sortedUniqBenchmarks.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const _ = require('lodash'); | |
// Alter these values to see how it affects the overall performance. | |
const NUMBER_OF_UNIQUE_ITEMS = 100; | |
const NUMBER_OF_DUPLICATES_PER_ITEM = 10000; | |
const TRIALS = 100; | |
function buildArray() { | |
const result = []; | |
for (let i = 0; i < NUMBER_OF_UNIQUE_ITEMS; ++i) { |
View exampleData.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { User } from './userManager'; | |
export const users: Map<string, User> = new Map(Object.entries({ | |
'1': { | |
id: '1', | |
username: 'admin', | |
permissions: ['VIEW', 'UPDATE'], | |
}, | |
'2': { | |
id: '2', |
View strategyPattern.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Monster { | |
#onAttack | |
constructor({ dropLoot, onAttack, customBehaviors = {} }) { | |
this.dropLoot = dropLoot | |
this.#onAttack = onAttack ?? (() => {}) | |
this.behaviors = customBehaviors | |
} | |
attack() { | |
playHurtAnimation() |
View runtimeHierarchyCheck.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const monsterSentinel = {} | |
export const isMonster = obj => obj.typeSentinel === monsterSentinel | |
export class Slime { | |
typeSentinel = monsterSentinel | |
... | |
} | |
export class Skeleton { |
View publicHelpers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Public helpers | |
export const monsterBehaviors = { | |
kill(monster) { | |
userInventory.add(monster.dropLoot()) | |
removeFromUi(this) | |
} | |
} | |
// private helpers | |
const monsterHelpers = { |
View restrictedInheritance.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const monsterHelpers = { | |
attack() { | |
playHurtAnimation() | |
} | |
} | |
class MonsterBehaviors { | |
#id = Math.random() | |
#dropLoot |
View classicComposition.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MonsterBehaviors { | |
#dropLoot | |
constructor({ dropLoot }) { | |
this.#dropLoot = dropLoot | |
} | |
attack() { | |
playHurtAnimation() | |
} |
View helperFunctions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const monsterBehaviors = { | |
attack() { | |
playHurtAnimation() | |
}, | |
die({ monsterId, dropLoot }) { | |
userInventory.add(dropLoot()) | |
removeFromUi(monsterId) | |
}, | |
} |
View originalInheritanceExample.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class Monster { | |
constructor() { | |
if (this.constructor === Monster) { | |
throw new Error('This is an abstract class. Do not instantiate it directly.'); | |
} | |
} | |
attack() { | |
playHurtAnimation() | |
} |
View exceptions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
// This module provides a light version of the explicit-exceptions API. | |
// You can find the original package this module was modeled after | |
// at this URL: https://www.npmjs.com/package/explicit-exceptions | |
/** | |
* An exception that can be thrown (or rethrown) within a function decorated with wrap(). | |
*/ | |
class Exception extends Error { |
NewerOlder