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

use Buffer.from if available(usually in node.js environments) in _.isEqual #2881

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion modules/isEqual.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import _ from './underscore.js';
import { toString, SymbolProto } from './_setup.js';
import { toString, SymbolProto, root } from './_setup.js';
import getByteLength from './_getByteLength.js';
import isTypedArray from './isTypedArray.js';
import isFunction from './isFunction.js';
import keys from './keys.js';
import has from './_has.js';
import toDataView from './_toDataView.js';
import bind from './bind.js';

// Internal recursive comparison function for `_.isEqual`.
function eq(a, b, aStack, bStack) {
Expand All @@ -22,6 +23,9 @@ function eq(a, b, aStack, bStack) {
return deepEq(a, b, aStack, bStack);
}

// Get Buffer.from function, if present(usually in node.js environment)
var bufferFrom = root.Buffer && root.Buffer.from && isFunction(root.Buffer.from) && bind(root.Buffer.from, root.Buffer);

// Internal recursive comparison function for `_.isEqual`.
function deepEq(a, b, aStack, bStack) {
// Unwrap any wrapped objects.
Expand Down Expand Up @@ -53,6 +57,11 @@ function deepEq(a, b, aStack, bStack) {
case '[object Symbol]':
return SymbolProto.valueOf.call(a) === SymbolProto.valueOf.call(b);
case '[object ArrayBuffer]':
// If equals function is present(usually in nodejs environments), on the buffer object
// use that as that will be faster
if (isFunction(a.equals)) {
return a.equals(b);
}
// Coerce to `DataView` so we can fall through to the next case.
return deepEq(toDataView(a), toDataView(b), aStack, bStack);
case '[object DataView]':
Expand All @@ -68,6 +77,11 @@ function deepEq(a, b, aStack, bStack) {
}

if (isTypedArray(a)) {
// If Buffer.from function is present(usually in nodejs environments), use that as
// that will be faster
if (bufferFrom) {
return bufferFrom(a).equals(bufferFrom(b));
}
// Coerce typed arrays to `DataView`.
return deepEq(toDataView(a), toDataView(b), aStack, bStack);
}
Expand Down
87 changes: 50 additions & 37 deletions underscore-esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion underscore-esm.js.map

Large diffs are not rendered by default.

87 changes: 50 additions & 37 deletions underscore.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion underscore.js.map

Large diffs are not rendered by default.