-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
core(computed-artifact): use deep equality over strict #4409
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* @license Copyright 2018 Google Inc. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
const isEqual = require('lodash.isequal'); | ||
|
||
/** | ||
* @fileoverview This class is designed to allow maps with arbitrary equality functions. | ||
* It is not meant to be performant and is well-suited to use cases where the number of entries is | ||
* likely to be small (like computed artifacts). | ||
*/ | ||
module.exports = class CacheMap { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for the record, I'm still not a huge fan of this name, something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm fine with calling it ArbitraryEqualityMap. |
||
constructor() { | ||
this._equalsFn = (a, b) => a === b; | ||
this._entries = []; | ||
} | ||
|
||
/** | ||
* @param {function():boolean} equalsFn | ||
*/ | ||
setEqualsFn(equalsFn) { | ||
this._equalsFn = equalsFn; | ||
} | ||
|
||
has(key) { | ||
return this._findIndexOf(key) !== -1; | ||
} | ||
|
||
get(key) { | ||
const entry = this._entries[this._findIndexOf(key)]; | ||
return entry && entry.value; | ||
} | ||
|
||
set(key, value) { | ||
let index = this._findIndexOf(key); | ||
if (index === -1) index = this._entries.length; | ||
this._entries[index] = {key, value}; | ||
} | ||
|
||
_findIndexOf(key) { | ||
for (let i = 0; i < this._entries.length; i++) { | ||
if (this._equalsFn(key, this._entries[i].key)) return i; | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
/** | ||
* Determines whether two objects are deeply equal. Defers to lodash isEqual, but is kept here for | ||
* easy usage by consumers. | ||
* See https://lodash.com/docs/4.17.5#isEqual. | ||
* @param {*} objA | ||
* @param {*} objB | ||
* @return {boolean} | ||
*/ | ||
static deepEquals(objA, objB) { | ||
return isEqual(objA, objB); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* @license Copyright 2018 Google Inc. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
/* eslint-env mocha */ | ||
|
||
const assert = require('assert'); | ||
const CacheMap = require('../../lib/cache-map.js'); | ||
const trace = require('../fixtures/traces/progressive-app-m60.json'); | ||
|
||
describe('CacheMap', () => { | ||
it('creates a map', () => { | ||
const map = new CacheMap(); | ||
assert.equal(map.has(1), false); | ||
assert.equal(map.get(1), undefined); | ||
map.set(1, 2); | ||
assert.equal(map.has(1), true); | ||
assert.equal(map.get(1), 2); | ||
}); | ||
|
||
it('uses custom equality function', () => { | ||
// create a map which stores 1 value per type | ||
const map = new CacheMap(); | ||
map.setEqualsFn((a, b) => typeof a === typeof b); | ||
map.set(true, 1); | ||
map.set('foo', 2); | ||
map.set({}, 3); | ||
map.set('bar', 4); | ||
|
||
assert.equal(map.has(1), false); | ||
assert.equal(map.has(false), true); | ||
assert.equal(map.has(''), true); | ||
assert.equal(map.has({x: 1}), true); | ||
assert.equal(map.get('foo'), 4); | ||
}); | ||
|
||
it('is not hella slow', () => { | ||
const map = new CacheMap(); | ||
map.setEqualsFn(CacheMap.deepEquals); | ||
for (let i = 0; i < 100; i++) { | ||
map.set({i}, i); | ||
} | ||
|
||
for (let j = 0; j < 1000; j++) { | ||
const i = j % 100; | ||
assert.equal(map.get({i}), i); | ||
} | ||
}).timeout(1000); | ||
|
||
it('is fast for expected usage', () => { | ||
const map = new CacheMap(); | ||
map.setEqualsFn(CacheMap.deepEquals); | ||
map.set([trace, {x: 0}], 'foo'); | ||
map.set([trace, {x: 1}], 'bar'); | ||
|
||
for (let i = 0; i < 10000; i++) { | ||
assert.equal(map.get([trace, {x: i % 2}]), i % 2 ? 'bar' : 'foo'); | ||
} | ||
}).timeout(1000); | ||
}); |
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.
setEqualityFn