1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-09-02 19:52:32 +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 = { module.exports = {
setup(state) { setup(state) {
window.__NO_MEMOIZE = false
let obj = { let obj = {
fibonacci(n = 20) { fibonacci(n = 20) {
if (n === 0 || n === 1) { if (n === 0 || n === 1) {
@@ -20,11 +18,5 @@ module.exports = {
run(obj) { run(obj) {
obj.fibonacci() 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 _ = require('lodash')
const readMetadata = require('read-metadata') const readMetadata = require('read-metadata')
const { Raw } = require('..') const { Raw } = require('..')
const { resolve } = require('path') const memoize = require('../lib/utils/memoize')
window.__NO_MEMOIZE = true const { resolve } = require('path')
const DEFAULT_BENCHMARK = { const DEFAULT_BENCHMARK = {
setup(state) { return state }, setup(state) { return state },
@@ -77,6 +77,7 @@ function runBenchmarks() {
// Setup global scope for this benchmark // Setup global scope for this benchmark
global.setScope(benchmarkName, { global.setScope(benchmarkName, {
Raw, Raw,
memoize,
benchmark, benchmark,
input input
}) })
@@ -93,15 +94,11 @@ function runBenchmarks() {
// Time spent in setup is not taken into account // Time spent in setup is not taken into account
setup() { setup() {
// Create as much independant Slate.State as needed, to avoid
// memoization between calls to `fn`
const scope = global.getScope() const scope = global.getScope()
// Each benchmark is given the chance to do its own setup
const state = const state = scope.benchmark.setup(
// Each benchmark is given the chance to do its own setup scope.Raw.deserialize(scope.input, { terse: true })
scope.benchmark.setup( )
scope.Raw.deserialize(scope.input, { terse: true })
)
}, },
// Because of the way BenchmarkJS compiles the functions, // Because of the way BenchmarkJS compiles the functions,
@@ -109,7 +106,8 @@ function runBenchmarks() {
fn() { fn() {
scope.benchmark.run(state) // eslint-disable-line no-undef 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() { onComplete() {

View File

@@ -1,5 +1,18 @@
/* global Map */ /* 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. * 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) { object[property] = function (...args) {
if (window.__NO_MEMOIZE) { if (IS_DEV) {
return original.apply(this, args) 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] const keys = [property, ...args]
@@ -118,8 +138,34 @@ function getIn(map, keys) {
return childMap.get(LEAF) 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.
*/ */
export default memoize export {
memoize as default,
__clear,
__enable
}