-
-
Notifications
You must be signed in to change notification settings - Fork 511
Record: optimise filterWithIndex (conservative) and traverseWithKey (less object creations) (fixes #711) #725
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
Conversation
Ah! Adding "jest": {
"testURL": "http://localhost/"
}in |
|
@sledorze actually we could upgrade to |
| */ | ||
| export function filterWithIndex<K extends string, A>(fa: Record<K, A>, p: (key: K, a: A) => boolean): Record<string, A> | ||
| export function filterWithIndex<A>(fa: Record<string, A>, p: (key: string, a: A) => boolean): Record<string, A> | ||
| export function filterWithIndex<A>(fa: Record<string, A>, p: (key: string, a: A) => boolean): Record<string, A> { |
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.
What about
export function filterWithIndex<A>(fa: Record<string, A>, p: (key: string, a: A) => boolean): Record<string, A> {
const r: Record<string, A> = {}
let changed = false
for (const key in fa) {
if (fa.hasOwnProperty(key)) {
const a = fa[key]
if (p(key, a)) {
r[key] = a
} else {
changed = true
}
}
}
return changed ? r : fa
}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.
It's doing all work in case of noop (obj création plus assigns (n log n))
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.
noop is unlikely in a filter* function so in the most common case looks like you are doing even more work
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 fine with the simpler approach.
Afk this morning (on phone) so expect a change later.
test/Record.ts
Outdated
| const actual = R.filter(y, isNumber) | ||
| assert.deepStrictEqual(actual, { a: 1 }) | ||
|
|
||
| const z: Record<string, string | number> = { b: 1, a: 'foo', c: 'bar' } |
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.
This test doesn't look different from the previous one
|
Was to trigger another case for coverage |
|
@gcanti yes adding Solves the problem (I have another one popping, however) |
|
FYI we are upgrading jest in the other PR #715 (comment) |
|
@gcanti not sure how I can fix that coverage issue |
|
@sledorze did you try EDIT: specifically |
| r[key] = a | ||
| let changed = false | ||
| for (const key in fa) { | ||
| if (fa.hasOwnProperty(key)) { |
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.
ah sorry, do you mean this line right?
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.
yes
|
this seems to work const x = Object.assign(Object.create({ c: true }), { a: 1, b: 'foo' })
assert.deepStrictEqual(R.filter(x, isNumber), { a: 1 }) |
|
thanks @gcanti |
No description provided.