diff --git a/perf/index.js b/perf/index.js index 10497782e..b69f33ccc 100644 --- a/perf/index.js +++ b/perf/index.js @@ -20,6 +20,8 @@ const fs = require('fs') const _ = require('lodash') const readMetadata = require('read-metadata') const { Raw } = require('..') +const memoize = require('../lib/utils/memoize') + const { resolve } = require('path') const DEFAULT_BENCHMARK = { @@ -75,6 +77,7 @@ function runBenchmarks() { // Setup global scope for this benchmark global.setScope(benchmarkName, { Raw, + memoize, benchmark, input }) @@ -91,31 +94,25 @@ function runBenchmarks() { // Time spent in setup is not taken into account setup() { - // Create as much independant Slate.State as needed, to avoid - // memoization between calls to `fn` const scope = global.getScope() - - let states = [] - let numberOfExecution = this.count - while (numberOfExecution--) { - states.push( - // Each benchmark is given the chance to do its own setup - scope.benchmark.setup( - scope.Raw.deserialize(scope.input, { terse: true }) - ) - ) - } - - let stateIndex = 0 + // Each benchmark is given the chance to do its own setup + const state = scope.benchmark.setup( + scope.Raw.deserialize(scope.input, { terse: true }) + ) }, // Because of the way BenchmarkJS compiles the functions, // the variables declared in `setup` are visible to `fn` fn() { - scope.benchmark.run(states[stateIndex]) // eslint-disable-line no-undef - // Next call will use another State instance - stateIndex++ // eslint-disable-line no-undef + scope.benchmark.run(state) // eslint-disable-line no-undef + // Clear memoized values between each run + scope.memoize.__clear() // eslint-disable-line no-undef + }, + + onComplete() { + const teardown = global.getScope().benchmark.teardown + if (teardown) teardown() } })) } diff --git a/src/utils/memoize.js b/src/utils/memoize.js index c49bda1c7..2dcd53671 100644 --- a/src/utils/memoize.js +++ b/src/utils/memoize.js @@ -1,5 +1,18 @@ /* global Map */ +import IS_DEV from './is-dev' + +/** + * This module serves to memoize methods on immutable instances. + */ + +// Global: True if memoization should is enabled. Only effective in DEV mode +let ENABLED = true + +// Global: Changing this cache key will clear all previous cached +// results. Only effective in DEV mode +let CACHE_KEY = 0 + /** * The leaf node of a cache tree. Used to support variable argument length. * @@ -42,6 +55,17 @@ function memoize(object, properties) { } object[property] = function (...args) { + if (IS_DEV) { + if (!ENABLED) { + // Memoization disabled + return original.apply(this, args) + } else if (CACHE_KEY !== this.__cache_key) { + // Previous caches must be cleared + this.__cache_key = CACHE_KEY + this.__cache = new Map() + } + } + const keys = [property, ...args] this.__cache = this.__cache || new Map() @@ -114,8 +138,34 @@ function getIn(map, keys) { return childMap.get(LEAF) } +/** + * In DEV mode, clears the previously memoized values, globally. + * @return {Void} + */ + +function __clear() { + CACHE_KEY++ + if (CACHE_KEY >= Number.MAX_SAFE_INTEGER) { + CACHE_KEY = 0 + } +} + +/** + * In DEV mode, enable or disable the use of memoize values, globally. + * @param {Boolean} enabled + * @return {Void} + */ + +function __enable(enabled) { + ENABLED = enabled +} + /** * Export. */ -export default memoize +export { + memoize as default, + __clear, + __enable +}