Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 29 additions & 21 deletions benchmark/cache/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
const ora = require('ora')
const Table = require('cli-table2')
const debug = require('logdown')()
const Benchmark = require('benchmark')
import ora from 'ora'
import Table from 'cli-table2'
import logdown from 'logdown'
import Benchmark from 'benchmark'
import mapCache from './map.js'
import objectCache from './object.js'
import objectWithoutProtoCache from './object-without-prototype.js'
import lruCache from './lru-cache.js'
import stringifySerializer from '../serializer/json-stringify.js'
import partialApplicationStrategy from '../strategy/partial-application.js'

const debug = logdown()

const results = []
const spinner = ora('Running benchmark')
Expand All @@ -11,11 +19,11 @@ const spinner = ora('Running benchmark')
//

function showResults (benchmarkResults) {
const table = new Table({head: ['NAME', 'OPS/SEC', 'RELATIVE MARGIN OF ERROR', 'SAMPLE SIZE']})
const table = new Table({ head: ['NAME', 'OPS/SEC', 'RELATIVE MARGIN OF ERROR', 'SAMPLE SIZE'] })
benchmarkResults.forEach((result) => {
table.push([
result.target.name,
result.target.hz.toLocaleString('en-US', {maximumFractionDigits: 0}),
result.target.hz.toLocaleString('en-US', { maximumFractionDigits: 0 }),
`± ${result.target.stats.rme.toFixed(2)}%`,
result.target.stats.sample.length
])
Expand Down Expand Up @@ -47,35 +55,35 @@ spinner.start()
// Benchmark
//

let fibonacci = (n) => {
const fibonacci = (n) => {
return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2)
}

let caches = []
caches.push(require('./map'))
caches.push(require('./object'))
caches.push(require('./object-without-prototype'))
caches.push(require('./lru-cache'))
const caches = []
caches.push(mapCache)
caches.push(objectCache)
caches.push(objectWithoutProtoCache)
caches.push(lruCache)

let serializers = []
serializers.push(require('../serializer/json-stringify'))
const serializers = []
serializers.push(stringifySerializer)

let strategies = []
strategies.push(require('../strategy/partial-application'))
const strategies = []
strategies.push(partialApplicationStrategy)

let memoizedFunctions = []
const memoizedFunctions = []
strategies.forEach(function (strategy) {
serializers.forEach(function (serializer) {
caches.forEach(function (cache) {
let memoizedFibonacci = strategy(fibonacci, {cache, serializer})
const memoizedFibonacci = strategy(fibonacci, { cache, serializer })
memoizedFibonacci.label = cache.label
memoizedFunctions.push(memoizedFibonacci)
})
})
})

let suiteFibonnaci = new Benchmark.Suite()
let fibNumber = 15
const suiteFibonnaci = new Benchmark.Suite()
const fibNumber = 15

memoizedFunctions.forEach(function (memoizedFunction) {
suiteFibonnaci.add(memoizedFunction.label, () => memoizedFunction(fibNumber))
Expand All @@ -84,4 +92,4 @@ memoizedFunctions.forEach(function (memoizedFunction) {
suiteFibonnaci
.on('cycle', onCycle)
.on('complete', onComplete)
.run({'async': true})
.run({ async: true })
4 changes: 2 additions & 2 deletions benchmark/cache/lru-cache.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var LruCache = require('lru-cache')
import LruCache from 'lru-cache'

module.exports = {
export default {
create: () => new LruCache(),
label: 'lru-cache'
}
8 changes: 4 additions & 4 deletions benchmark/cache/map.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
function hasSupport () {
var hasSupport = true
let hasSupport = true

try {
var map = new Map()
const map = new Map()
map.set(null)
} catch (error) {
hasSupport = false
Expand All @@ -12,11 +12,11 @@ function hasSupport () {
}

function create () {
var cache = new Map()
const cache = new Map()
return cache
}

module.exports = {
export default {
create: create,
hasSupport: hasSupport,
label: 'Map'
Expand Down
2 changes: 1 addition & 1 deletion benchmark/cache/object-without-prototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ObjectWithoutPrototypeCache {
}
}

module.exports = {
export default {
create: () => new ObjectWithoutPrototypeCache(),
label: 'Object without prototype'
}
2 changes: 1 addition & 1 deletion benchmark/cache/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ObjectCache {
}
}

module.exports = {
export default {
create: () => new ObjectCache(),
label: 'Object'
}
51 changes: 32 additions & 19 deletions benchmark/combination.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
const ora = require('ora')
const debug = require('logdown')()
const Table = require('cli-table2')
const Benchmark = require('benchmark')
import ora from 'ora'
import logdown from 'logdown'
import Table from 'cli-table2'
import Benchmark from 'benchmark'
import mapCache from './map.js'
import objectCache from './object.js'
import objectWithoutProtoCache from './object-without-prototype.js'
import lruCache from './lru-cache.js'
import stringifySerializer from '../serializer/json-stringify.js'
import partialApplicationStrategy from '../strategy/partial-application.js'
import stringifyBindedSerializer from '../serializer/json-stringify-binded.js'
import utilInspectSerializer from '../serializer/util-inspect.js'
import naiveStrategy from '../strategy/naive.js'
import singleArgumentStrategy from './strategy/optimize-for-single-argument.js'
import inferArityStrategy from './strategy/infer-arity.js'

const debug = logdown()

const results = []

Expand All @@ -10,11 +23,11 @@ const results = []
//

function showResults (results) {
const table = new Table({head: ['NAME', 'OPS/SEC', 'RELATIVE MARGIN OF ERROR', 'SAMPLE SIZE']})
const table = new Table({ head: ['NAME', 'OPS/SEC', 'RELATIVE MARGIN OF ERROR', 'SAMPLE SIZE'] })
results.forEach((result) => {
table.push([
result.target.name,
result.target.hz.toLocaleString('en-US', {maximumFractionDigits: 0}),
result.target.hz.toLocaleString('en-US', { maximumFractionDigits: 0 }),
`± ${result.target.stats.rme.toFixed(2)}%`,
result.target.stats.sample.length
])
Expand Down Expand Up @@ -53,27 +66,27 @@ const fibonacci = (n) => {
}

const caches = []
caches.push(require('./cache/map'))
caches.push(require('./cache/object'))
caches.push(require('./cache/object-without-prototype'))
caches.push(require('./cache/lru-cache'))
caches.push(mapCache)
caches.push(objectCache)
caches.push(objectWithoutProtoCache)
caches.push(lruCache)

const serializers = []
serializers.push(require('./serializer/json-stringify'))
serializers.push(require('./serializer/json-stringify-binded'))
serializers.push(require('./serializer/util-inspect'))
serializers.push(stringifySerializer)
serializers.push(stringifyBindedSerializer)
serializers.push(utilInspectSerializer)

const strategies = []
strategies.push(require('./strategy/naive'))
strategies.push(require('./strategy/optimize-for-single-argument'))
strategies.push(require('./strategy/infer-arity'))
strategies.push(require('./strategy/partial-application'))
strategies.push(naiveStrategy)
strategies.push(singleArgumentStrategy)
strategies.push(inferArityStrategy)
strategies.push(partialApplicationStrategy)

const memoizedFunctions = []
strategies.forEach(function (strategy) {
serializers.forEach(function (serializer) {
caches.forEach(function (cache) {
memoizedFunctions.push(strategy(fibonacci, {cache, serializer}))
memoizedFunctions.push(strategy(fibonacci, { cache, serializer }))
})
})
})
Expand All @@ -93,6 +106,6 @@ suiteFibonnaci
onCycle(event)
})
.on('complete', onComplete)
.run({'async': true})
.run({ async: true })

spinner.start()
10 changes: 5 additions & 5 deletions benchmark/compare-commits/benchmark-solo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Benchmark = require('benchmark')
const fastMemoize = require('../../src')
import Benchmark from 'benchmark'
import { memoize } from '../../src/index.js'

const benchmarkResults = []

Expand Down Expand Up @@ -29,17 +29,17 @@ const fibonacci = (n) => {
return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2)
}

const memoizedFastMemoizeCurrentVersion = fastMemoize(fibonacci)
const memoizedFastMemoizeCurrentVersion = memoize(fibonacci)

const benchmark = new Benchmark.Suite()
const fibNumber = 15

benchmark
.add(`fast-memoize@current`, () => {
.add('fast-memoize@current', () => {
memoizedFastMemoizeCurrentVersion(fibNumber)
})
.on('cycle', (event) => {
benchmarkResults.push(event)
})
.on('complete', onComplete)
.run({'async': true})
.run({ async: true })
6 changes: 3 additions & 3 deletions benchmark/compare-commits/output-result.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const Table = require('cli-table2')
import Table from 'cli-table2'

const formatResult = (result) => {
return {
operationsPerSecond: result.operationsPerSecond.toLocaleString('en-US', {maximumFractionDigits: 0}),
operationsPerSecond: result.operationsPerSecond.toLocaleString('en-US', { maximumFractionDigits: 0 }),
relativeMarginOfError: `± ${Number(result.relativeMarginOfError).toFixed(2)}%`,
sampleSize: result.sampleSize
}
Expand All @@ -12,7 +12,7 @@ const result1 = formatResult(JSON.parse(process.argv[2])) // current
const result2 = formatResult(JSON.parse(process.argv[3]))
const commit = process.argv[4]

const table = new Table({head: ['GIT', 'OPS/SEC', 'RELATIVE MARGIN OF ERROR', 'SAMPLE SIZE']})
const table = new Table({ head: ['GIT', 'OPS/SEC', 'RELATIVE MARGIN OF ERROR', 'SAMPLE SIZE'] })
table.push([
'HEAD',
result1.operationsPerSecond,
Expand Down
41 changes: 23 additions & 18 deletions benchmark/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
const ora = require('ora')
const Table = require('cli-table2')
const debug = require('logdown')()
const lruMemoize = require('lru-memoize').default
const iMemoized = require('iMemoized')
const Benchmark = require('benchmark')
const underscore = require('underscore').memoize
const lodash = require('lodash').memoize
const memoizee = require('memoizee')
const R = require('ramda')
const nano = require('nano-memoize')
const fastMemoize = require('../src/')
import ora from 'ora'
import Table from 'cli-table2'
import logdown from 'logdown'
import lru from 'lru-memoize'
import iMemoized from 'iMemoized'
import Benchmark from 'benchmark'
import underscore from 'underscore'
import lodash from 'lodash'
import memoizee from 'memoizee'
import R from 'ramda'
import nano from 'nano-memoize'
import { memoize as fastMemoize } from '../src/index.js'

const debug = logdown()
const lodashMemoize = lodash.memoize;
const underscoreMemoize = underscore.memoize;
const lruMemoize = lru.default;

const results = []
const spinner = ora('Running benchmark')
Expand All @@ -19,11 +24,11 @@ const spinner = ora('Running benchmark')
//

function showResults (benchmarkResults) {
const table = new Table({head: ['NAME', 'OPS/SEC', 'RELATIVE MARGIN OF ERROR', 'SAMPLE SIZE']})
const table = new Table({ head: ['NAME', 'OPS/SEC', 'RELATIVE MARGIN OF ERROR', 'SAMPLE SIZE'] })
benchmarkResults.forEach((result) => {
table.push([
result.target.name,
result.target.hz.toLocaleString('en-US', {maximumFractionDigits: 0}),
result.target.hz.toLocaleString('en-US', { maximumFractionDigits: 0 }),
`± ${result.target.stats.rme.toFixed(2)}%`,
result.target.stats.sample.length
])
Expand Down Expand Up @@ -62,8 +67,8 @@ const fibonacci = (n) => {
const fibNumber = 15
const fibCount = 1973

const memoizedUnderscore = underscore(fibonacci)
const memoizedLodash = lodash(fibonacci)
const memoizedUnderscore = underscoreMemoize(fibonacci)
const memoizedLodash = lodashMemoize(fibonacci)
const memoizedMemoizee = memoizee(fibonacci)
const memoizedRamda = R.memoize(fibonacci)
const memoizedImemoized = iMemoized.memoize(fibonacci)
Expand Down Expand Up @@ -102,9 +107,9 @@ benchmark
.add('nano-memoize', () => {
memoizedNano(fibNumber)
})
.add(`fast-memoize@current`, () => {
.add('fast-memoize@current', () => {
memoizedFastMemoizeCurrentVersion(fibNumber)
})
.on('cycle', onCycle)
.on('complete', onComplete)
.run({'async': true})
.run({ async: true })
Loading