Skip to content

Commit

Permalink
memoize getValueCache calls
Browse files Browse the repository at this point in the history
  • Loading branch information
anajavi committed Oct 31, 2018
1 parent 4a9028f commit 6cedd7e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
],
"@babel/react"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
],
"env": {
"test": {
"presets": [
Expand Down
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"parser": "babel-eslint",
"extends": [
"plugin:react/recommended",
"airbnb",
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@
"react": "^0.14.0 || ^15.0.0 || ^16.0.0"
},
"dependencies": {
"memoize-one": "^4.0.2",
"prop-types": "^15.6.2"
},
"devDependencies": {
"@babel/cli": "^7.1.2",
"@babel/core": "^7.1.2",
"@babel/plugin-external-helpers": "^7.0.0",
"@babel/plugin-proposal-class-properties": "^7.1.0",
"@babel/preset-env": "^7.1.0",
"@babel/preset-react": "^7.0.0",
"autoprefixer": "^9.1.3",
Expand Down
11 changes: 6 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import PropTypes from 'prop-types';
import memoizeOne from 'memoize-one';
import { DAYS_IN_WEEK, MILLISECONDS_IN_ONE_DAY, DAY_LABELS, MONTH_LABELS } from './constants';
import {
dateNDaysAgo,
Expand Down Expand Up @@ -94,8 +95,8 @@ class CalendarHeatmap extends React.Component {
);
}

getValueCache(values) {
return values.reduce((memo, value) => {
getValueCache = memoizeOne((props) =>
props.values.reduce((memo, value) => {
const date = convertToDate(value.date);
const index = Math.floor((date - this.getStartDateWithEmptyDays()) / MILLISECONDS_IN_ONE_DAY);
// eslint-disable-next-line no-param-reassign
Expand All @@ -106,8 +107,8 @@ class CalendarHeatmap extends React.Component {
tooltipDataAttrs: this.getTooltipDataAttrsForValue(value),
};
return memo;
}, {});
}
}, {}),
);

getValueForIndex(index) {
if (this.valueCache[index]) {
Expand Down Expand Up @@ -308,7 +309,7 @@ class CalendarHeatmap extends React.Component {
}

render() {
this.valueCache = this.getValueCache(this.props.values);
this.valueCache = this.getValueCache(this.props);

return (
<svg className="react-calendar-heatmap" viewBox={this.getViewBox()}>
Expand Down

0 comments on commit 6cedd7e

Please sign in to comment.