-
Notifications
You must be signed in to change notification settings - Fork 75
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
Memoize convertColor to add speed. #936
Conversation
b0a0b43
to
2037c67
Compare
ad557e0
to
2b8ae88
Compare
* @param {geo.geoColorObject} resultColor The result of the conversion. | ||
* @returns {geo.geoColorObject} The `resultColor`. | ||
*/ | ||
function memoizeConvertColor(origColor, resultColor) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we call it cacheConvertColor essentially using cache instead of memoize, as it may provide consistent naming convention elsewhere, memoize is fine as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd argue that memoization is a specific type of cache specifically when you cache just the return value of a function. Here, I think we are doing memoization. In the tile layer, we are also loading the tiles and handling asynchronous factors, so there I think it is caching but not memoization.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, lets go with memoization, I do not see the distinction but it seems that others may. +1
*/ | ||
function memoizeConvertColor(origColor, resultColor) { | ||
m_memoizeConvertColor.count += 1; | ||
if (m_memoizeConvertColor.count >= m_memoizeConvertColor.maxCount) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice to have maxCount +1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what you mean here. If the memoize cache has maxCount values currently, adding another value will exceed it, so we reset before adding another value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant that I liked this check
@@ -115,6 +115,28 @@ describe('geo.util.convertColor', function () { | |||
expect(geo.util.convertColor(undefined)).toBe(undefined); | |||
}); | |||
}); | |||
describe('Memoization', function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for some improved reading of the test outputs, I would use Memoization Colors - as we may have more memoization in the future
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test output looks like:
geo.util.convertColor
From strings
✓ #000000
✓ #ffffff
...
✓ TRANSPARENT
✓ none
From hex value
✓ 0x000000
✓ 0xffffff
✓ 0x1256ab
Pass through unknown colors
✓ object
✓ undefined
Memoization
✓ Response is memoized
✓ Memoization gets reset
So this is already under geo.util.convertColor
. I think that it is clear enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly looks great, just had some minor suggestions on naming - which could be ignored.
2b8ae88
to
bb7b190
Compare
Also, refactor util/index.js to have color utilities in their own file and other common utilities in util/common.js. Named CSS colors are now taken from the npm color-name package instead of including them ourselves.
bb7b190
to
6e27b1a
Compare
Also, refactor util/index.js to have color utilities in their own file and other common utilities in util/common.js.