-
Notifications
You must be signed in to change notification settings - Fork 30k
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
dgram: prefer strict equality #8011
Closed
claudiorodriguez
wants to merge
1
commit into
nodejs:master
from
claudiorodriguez:dgram-prefer-strict-equal
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
const dgram = require('dgram'); | ||
const invalidTypes = [ | ||
'test', | ||
['udp4'], | ||
new String('udp4'), | ||
1, | ||
{}, | ||
true, | ||
false, | ||
null, | ||
undefined | ||
]; | ||
const validTypes = [ | ||
'udp4', | ||
'udp6', | ||
{ type: 'udp4' }, | ||
{ type: 'udp6' } | ||
]; | ||
|
||
// Error must be thrown with invalid types | ||
invalidTypes.forEach((invalidType) => { | ||
assert.throws(() => { | ||
dgram.createSocket(invalidType); | ||
}, /Bad socket type specified/); | ||
}); | ||
|
||
// Error must not be thrown with valid types | ||
validTypes.forEach((validType) => { | ||
assert.doesNotThrow(() => { | ||
const socket = dgram.createSocket(validType); | ||
socket.close(); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 think V8 likes it better to have
typeof
checks come first.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.
@addaleax do you have any docs on this? Not saying it's not the case - I don't really know myself, but a quick google search throws nothing, and in the lib codebase both cases seem to be equally prevalent:
node/lib/https.js
Line 53 in be68b68
node/lib/dns.js
Line 111 in 013d76c
vs
node/lib/querystring.js
Line 172 in 6979632
node/lib/events.js
Line 63 in b7a8a69
if we can confirm one case is better, this could mean future improvement PRs and more consistency over the codebase
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.
@claudiorodriguez I’ll try to look it up/benchmark it, all I could remember right now is emberjs/ember.js#14118 … might be a different case with
!== null
, but even so I think I’d find thetypeof
-first variants more readable.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.
@claudiorodriguez This does indeed seem to be different with
!== null
. You can feel free to disregard my comment :)Expressions of the form
a && typeof a === 'object'
definitely seem to be a reason for deopts, though.