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

modernize lib/rarity-map.js #1485

Merged
merged 4 commits into from Aug 27, 2018
Merged

modernize lib/rarity-map.js #1485

merged 4 commits into from Aug 27, 2018

Conversation

@jimmywarting
Copy link
Contributor

jimmywarting commented Aug 26, 2018

@jimmywarting jimmywarting changed the title modernize lib/rarity-map.jsj modernize lib/rarity-map.js Aug 26, 2018
self._pieces = []
self._torrent = torrent
self._numPieces = torrent.pieces.length
self._pieces = new Array(self._numPieces).fill(0)

This comment has been minimized.

Copy link
@DiegoRBaquero

DiegoRBaquero Aug 26, 2018

Member

Any benefits in doing this?

This comment has been minimized.

Copy link
@jimmywarting

jimmywarting Aug 26, 2018

Author Contributor

You probably don't need the initial .fill(0) in new Array(self._numPieces).fill(0) maybe just new Array(self._numPieces) will do

Reason why i did this was b/c new Array(n) is faster then writing to a literal array []
And using .fill(0) is faster then writing to each index

skarmavbild 2018-08-26 kl 22 01 14

also b/c this looks cleaner then

recalculate () {	
  this._pieces.fill(0)
}

then

recalculate () {	
  for (let i = 0; i < this._numPieces; ++i) {	
    this._pieces[i] = 0	
  }
}
var self = this
class RarityMap {
constructor (torrent) {
const self = this

This comment has been minimized.

Copy link
@DiegoRBaquero

DiegoRBaquero Aug 26, 2018

Member

We should stop using self

} else {
return -1
destroy () {
const self = this

This comment has been minimized.

Copy link
@DiegoRBaquero

DiegoRBaquero Aug 26, 2018

Member

Same here, no more self

let min = Infinity

for (let i = 0; i < this._numPieces; ++i) {
if (pieceFilterFunc && !pieceFilterFunc(i)) continue

This comment has been minimized.

Copy link
@DiegoRBaquero

DiegoRBaquero Aug 26, 2018

Member

Much better removing that trueFn

This comment has been minimized.

Copy link
@jimmywarting

jimmywarting Aug 26, 2018

Author Contributor

thanks

Copy link
Member

DiegoRBaquero left a comment

So clean, so nice. LGTM

constructor (torrent) {
this._torrent = torrent
this._numPieces = torrent.pieces.length
this._pieces = new Array(this._numPieces).fill(0)

This comment has been minimized.

Copy link
@DiegoRBaquero

DiegoRBaquero Aug 26, 2018

Member

What is the fill here for? If it's not needed I think it will just consume O(n) to fill it and can be removed.

@DiegoRBaquero DiegoRBaquero merged commit 4657279 into webtorrent:master Aug 27, 2018
3 checks passed
3 checks passed
WIP ready for review
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@lock lock bot locked as resolved and limited conversation to collaborators Nov 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants
You can’t perform that action at this time.