1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-09-01 19:22:35 +02:00

Do not use window for globals. Expose clear/disable functions in utils/memoize

This commit is contained in:
Soreine
2016-11-07 15:38:10 +01:00
parent bada4fa3f1
commit 1424103975
3 changed files with 58 additions and 22 deletions

View File

@@ -2,8 +2,6 @@ const { default: memoize } = require('../../../lib/utils/memoize')
module.exports = {
setup(state) {
window.__NO_MEMOIZE = false
let obj = {
fibonacci(n = 20) {
if (n === 0 || n === 1) {
@@ -20,11 +18,5 @@ module.exports = {
run(obj) {
obj.fibonacci()
// Clear cache for next runs
delete obj.__cache
},
teardown() {
window.__NO_MEMOIZE = true
}
}

View File

@@ -20,9 +20,9 @@ const fs = require('fs')
const _ = require('lodash')
const readMetadata = require('read-metadata')
const { Raw } = require('..')
const { resolve } = require('path')
const memoize = require('../lib/utils/memoize')
window.__NO_MEMOIZE = true
const { resolve } = require('path')
const DEFAULT_BENCHMARK = {
setup(state) { return state },
@@ -77,6 +77,7 @@ function runBenchmarks() {
// Setup global scope for this benchmark
global.setScope(benchmarkName, {
Raw,
memoize,
benchmark,
input
})
@@ -93,15 +94,11 @@ 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()
const state =
// Each benchmark is given the chance to do its own setup
scope.benchmark.setup(
scope.Raw.deserialize(scope.input, { terse: true })
)
// 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,
@@ -109,7 +106,8 @@ function runBenchmarks() {
fn() {
scope.benchmark.run(state) // eslint-disable-line no-undef
// Next call will use another State instance
// Clear memoized values between each run
scope.memoize.__clear() // eslint-disable-line no-undef
},
onComplete() {

View File

@@ -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,8 +55,15 @@ function memoize(object, properties) {
}
object[property] = function (...args) {
if (window.__NO_MEMOIZE) {
return original.apply(this, 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]
@@ -118,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
}