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

rewrite groupByPlacement to avoid multiple scans of array #918

Merged
merged 3 commits into from
Feb 7, 2017
Merged
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: 8 additions & 8 deletions src/adapters/rhythmone.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ module.exports = function(bidManager, global, loader){
callback(200, "success", response.responseText);
else
callback(-1, "http error "+response.status, response.responseText);
}, false, {method:"GET"});
}, false, {method:"GET", withCredentials: true});
}
else{
loader(url, function(responseText, response){
if(response.status === 200)
callback(200, "success", response.responseText);
else
callback(-1, "http error "+response.status, response.responseText);
}, postData, {method:"POST", contentType: "application/json"});
}, postData, {method:"POST", contentType: "application/json", withCredentials: true});
}
}

Expand Down Expand Up @@ -115,6 +115,7 @@ module.exports = function(bidManager, global, loader){
function noBids(params){
for(var i=0; i<params.bids.length; i++){
if(params.bids[i].success !== 1){
logToConsole("registering nobid for slot "+params.bids[i].placementCode);
var bid = bidfactory.createBid(CONSTANTS.STATUS.NO_BID);
bid.bidderCode = bidderCode;
track(debug, 'hb', 'bidResponse', 0);
Expand Down Expand Up @@ -260,6 +261,9 @@ module.exports = function(bidManager, global, loader){
bidParams = getBidParameters(params.bids);

debug = (bidParams !== null && bidParams.debug === true);

auctionEnded = false;
requestCompleted = false;

track(debug, 'hb', 'callBids');

Expand Down Expand Up @@ -290,8 +294,6 @@ module.exports = function(bidManager, global, loader){

logToConsole(txt);

var bidCount = 0;

if(code === -1)
track(debug, 'hb', 'rmpReplyFail', msg);
else{
Expand All @@ -316,7 +318,6 @@ module.exports = function(bidManager, global, loader){

track(debug, 'hb', 'bidResponse', 1);
bidManager.addBidResponse(placementCode, pbResponse);
bidCount++;
};

track(debug, 'hb', 'rmpReplySuccess');
Expand All @@ -331,13 +332,12 @@ module.exports = function(bidManager, global, loader){
}

// if no bids are successful, inform prebid
if(bidCount === 0)
noBids(params);
noBids(params);

// when all bids are complete, log a report
track(debug, 'hb', 'bidsComplete');
});

logToConsole("version: "+version);
};
};
};
24 changes: 7 additions & 17 deletions src/bidmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,25 +296,15 @@ function processCallbacks(callbackQueue, singleAdUnitCode) {
* groupByPlacement is a reduce function that converts an array of Bid objects
* to an object with placement codes as keys, with each key representing an object
* with an array of `Bid` objects for that placement
* @param prev previous value as accumulator object
* @param item current array item
* @param idx current index
* @param arr the array being reduced
* @returns {*} as { [adUnitCode]: { bids: [Bid, Bid, Bid] } }
*/
function groupByPlacement(prev, item, idx, arr) {
// this uses a standard "array to map" operation that could be abstracted further
if (item.adUnitCode in Object.keys(prev)) {
// if the adUnitCode key is present in the accumulator object, continue
return prev;
} else {
// otherwise add the adUnitCode key to the accumulator object and set to an object with an
// array of Bids for that adUnitCode
prev[item.adUnitCode] = {
bids: arr.filter(bid => bid.adUnitCode === item.adUnitCode)
};
return prev;
}
function groupByPlacement(bidsByPlacement, bid) {
if (!bidsByPlacement[bid.adUnitCode])
bidsByPlacement[bid.adUnitCode] = { bids: [] };

bidsByPlacement[bid.adUnitCode].bids.push(bid);

return bidsByPlacement;
}

/**
Expand Down