Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#302 memoize expensive operations #331

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
95 changes: 75 additions & 20 deletions src/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,38 @@ import React from 'react';
import createElement from './create-element';
import checkForListedLanguage from './checkForListedLanguage';

class Memoize {
constructor() {
this.cache = new Map();
}

update(key, valueFn, dependencies) {
console.log(arguments);
if (!this.cache.has(key)) {
const value = valueFn();
this.cache.set(key, { value, dependencies });

return this.cache.get(key).value;
}
const {
value: currentValue,
dependencies: currentDependenciesValues
} = this.cache.get(key);
if (
dependencies.some(
(dep, index) => dep !== currentDependenciesValues[index]
)
) {
const newValue = valueFn();
this.cache.set(key, { value: newValue, dependencies });
return newValue;
}
return currentValue;
}
}
const memo = new Memoize();
console.log(memo);

const newLineRegex = /\n/g;
function getNewLines(str) {
return str.match(newLineRegex);
Expand Down Expand Up @@ -334,7 +366,7 @@ export default function(defaultAstGenerator, defaultStyle) {
wrapLines,
wrapLongLines = false,
lineProps = {},
renderer,
renderer = defaultRenderer,
PreTag = 'pre',
CodeTag = 'code',
code = Array.isArray(children) ? children[0] : children,
Expand Down Expand Up @@ -382,32 +414,55 @@ export default function(defaultAstGenerator, defaultStyle) {
*/
if ((wrapLines === undefined && renderer) || wrapLongLines)
wrapLines = true;

renderer = renderer || defaultRenderer;
const defaultCodeValue = [{ type: 'text', value: code }];
const codeTree = getCodeTree({
astGenerator,
language,
code,
defaultCodeValue
});
const defaultCodeValue = memo.update(
'defaultCodeValue',
() => [{ type: 'text', value: code }],
[code]
);
console.log(defaultCodeValue, memo);
const codeTree = memo.update(
'codeTree',
() =>
getCodeTree({
astGenerator,
language,
code,
defaultCodeValue
}),
[astGenerator, language, code, defaultCodeValue]
);
if (codeTree.language === null) {
codeTree.value = defaultCodeValue;
}

// determine largest line number so that we can force minWidth on all linenumber elements
const largestLineNumber = codeTree.value.length + startingLineNumber;

const rows = processLines(
codeTree,
wrapLines,
lineProps,
showLineNumbers,
showInlineLineNumbers,
startingLineNumber,
largestLineNumber,
lineNumberStyle,
wrapLongLines
const rows = memo.update(
'processLines',
() =>
processLines(
codeTree,
wrapLines,
lineProps,
showLineNumbers,
showInlineLineNumbers,
startingLineNumber,
largestLineNumber,
lineNumberStyle,
wrapLongLines
),
[
codeTree,
wrapLines,
lineProps,
showLineNumbers,
showInlineLineNumbers,
startingLineNumber,
largestLineNumber,
lineNumberStyle,
wrapLongLines
]
);

if (wrapLongLines) {
Expand Down