Skip to content

Instantly share code, notes, and snippets.

@snuffyDev
Created March 9, 2022 13:49
Show Gist options
  • Save snuffyDev/4877f45b0f3cafc5a5715299eb3737c7 to your computer and use it in GitHub Desktop.
Save snuffyDev/4877f45b0f3cafc5a5715299eb3737c7 to your computer and use it in GitHub Desktop.
Untitled benchmark (https://jsbench.github.io/#4877f45b0f3cafc5a5715299eb3737c7) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Untitled benchmark</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2>
</body>
</html>
"use strict";
(function (factory) {
if (typeof Benchmark !== "undefined") {
factory(Benchmark);
} else {
factory(require("benchmark"));
}
})(function (Benchmark) {
var suite = new Benchmark.Suite;
Benchmark.prototype.setup = function () {
async function fetchData(){
try {
const response = await fetch('https://hidden-frost-e4ea.beatbump.workers.dev/');
const data = await response.json();
return data;
} catch(e) {
console.error(e);
throw new Error(e);
}
}
const DATASET = (async () =>await fetchData())();
function thumbnailTransformer(url) {
let output = {
placeholder: '',
url: ''
};
if (!url.includes('lh3.googleusercontent.com')) {
const split_url = url.split('?');
const webp_url = split_url[0];
output.url = webp_url;
output.placeholder = webp_url?.replace('sddefault', 'default');
// console.log(output.placeholder, output.url, webp_url)
} else {
const webp_url = url?.replace('-rj', '-rw');
output.url = webp_url;
output.placeholder = webp_url?.replace(
/(=w(\d+)-h(\d+))/gm,
'=w1-h1-p-fSoften=50,50,05'
);
}
return output;
}
const MusicResponsiveListItemRenderer = (
ctx,
playlistSetVideoId,
playlistId
) => {
let {
thumbnail: {
musicThumbnailRenderer: { thumbnail: { thumbnails = [] } = {} } = {}
} = {}
} = ctx.musicResponsiveListItemRenderer;
for (let index = 0; index < thumbnails.length; index++) {
const thumbnail = thumbnails[index];
const { url, placeholder } = thumbnailTransformer(thumbnail.url);
thumbnails[index] = {
...thumbnail,
url,
original_url: thumbnail.url,
placeholder
};
}
let Item = {
subtitle:
Array.isArray(
ctx?.musicResponsiveListItemRenderer?.flexColumns[1]
?.musicResponsiveListItemFlexColumnRenderer?.text?.runs
) &&
(() => {
let arr = [];
let length =
ctx?.musicResponsiveListItemRenderer?.flexColumns[1]
?.musicResponsiveListItemFlexColumnRenderer?.text?.runs.length;
while (length--) {
arr[
length
] = ctx?.musicResponsiveListItemRenderer?.flexColumns[1]?.musicResponsiveListItemFlexColumnRenderer?.text?.runs[
length
]?.navigationEndpoint?.browseEndpoint?.browseEndpointContextSupportedConfigs?.browseEndpointContextMusicConfig?.pageType.includes(
'ARTIST'
)
? {
text:
ctx?.musicResponsiveListItemRenderer?.flexColumns[1]
?.musicResponsiveListItemFlexColumnRenderer?.text?.runs[
length
].text,
browseId:
ctx?.musicResponsiveListItemRenderer?.flexColumns[1]
?.musicResponsiveListItemFlexColumnRenderer?.text?.runs[
length
].navigationEndpoint?.browseEndpoint?.browseId,
pageType:
ctx?.musicResponsiveListItemRenderer?.flexColumns[1]
?.musicResponsiveListItemFlexColumnRenderer?.text?.runs[
length
]?.navigationEndpoint?.browseEndpoint
?.browseEndpointContextSupportedConfigs
?.browseEndpointContextMusicConfig?.pageType
}
: ctx?.musicResponsiveListItemRenderer?.flexColumns[1]
?.musicResponsiveListItemFlexColumnRenderer?.text?.runs[length];
}
return arr;
})(),
artistInfo: {
artist: Array.isArray(
ctx?.musicResponsiveListItemRenderer?.flexColumns[1]
?.musicResponsiveListItemFlexColumnRenderer?.text?.runs
) && [
{
text:
ctx?.musicResponsiveListItemRenderer?.flexColumns[1]
?.musicResponsiveListItemFlexColumnRenderer?.text?.runs[0]?.text,
browseId:
ctx?.musicResponsiveListItemRenderer?.flexColumns[1]
?.musicResponsiveListItemFlexColumnRenderer?.text?.runs[0]
?.navigationEndpoint?.browseEndpoint?.browseId,
pageType:
ctx?.musicResponsiveListItemRenderer?.flexColumns[1]
?.musicResponsiveListItemFlexColumnRenderer?.text?.runs[0]
?.navigationEndpoint?.browseEndpoint
?.browseEndpointContextSupportedConfigs
?.browseEndpointContextMusicConfig?.pageType
}
]
},
explicit: ctx?.musicResponsiveListItemRenderer.badges ? true : false,
title:
ctx.musicResponsiveListItemRenderer?.flexColumns[0]
?.musicResponsiveListItemFlexColumnRenderer?.text?.runs[0]?.text,
aspectRatio: ctx.musicResponsiveListItemRenderer.flexColumnDisplayStyle,
playerParams:
ctx.musicResponsiveListItemRenderer.flexColumns[0]
?.musicResponsiveListItemFlexColumnRenderer?.text?.runs[0]
?.navigationEndpoint?.watchEndpoint?.playerParams,
musicVideoType:
ctx.musicResponsiveListItemRenderer.flexColumns[0]
?.musicResponsiveListItemFlexColumnRenderer?.text?.runs[0]
?.navigationEndpoint?.watchEndpoint?.watchEndpointMusicConfig
?.musicVideoType,
videoId:
ctx.musicResponsiveListItemRenderer?.flexColumns[0]
?.musicResponsiveListItemFlexColumnRenderer?.text?.runs[0]
?.navigationEndpoint?.watchEndpoint?.videoId || '',
playlistId: ctx.musicResponsiveListItemRenderer?.menu?.menuRenderer.items[0]
.menuNavigationItemRenderer?.navigationEndpoint?.watchEndpoint?.playlistId
? ctx.musicResponsiveListItemRenderer?.menu?.menuRenderer?.items[0]
?.menuNavigationItemRenderer.navigationEndpoint?.watchEndpoint
?.playlistId
: ctx?.musicResponsiveListItemRenderer.navigationEndpoint?.watchEndpoint,
thumbnails,
length:
ctx?.musicResponsiveListItemRenderer?.fixedColumns &&
ctx?.musicResponsiveListItemRenderer?.fixedColumns[0]
?.musicResponsiveListItemFixedColumnRenderer?.text?.runs.length
? ctx?.musicResponsiveListItemRenderer?.fixedColumns[0]
?.musicResponsiveListItemFixedColumnRenderer?.text?.runs[0]?.text
: undefined
};
if (Item !== undefined && playlistSetVideoId) {
Item = {
...Item,
playlistSetVideoId:
ctx.musicResponsiveListItemRenderer.playlistItemData
?.playlistSetVideoId ||
ctx.musicResponsiveListItemRenderer?.overlay
?.musicItemThumbnailOverlayRenderer.content?.musicPlayButtonRenderer
?.playNavigationEndpoint?.watchEndpoint?.playlistSetVideoId
};
Item.playlistId = playlistId;
}
Item = {
...Item,
musicVideoType:
ctx.musicResponsiveListItemRenderer?.flexColumns[0]
?.musicResponsiveListItemFlexColumnRenderer?.text?.runs[0]
?.navigationEndpoint?.watchEndpoint?.watchEndpointMusicSupportedConfigs
?.watchEndpointMusicConfig?.musicVideoType
};
Item = {
...Item,
playerParams:
ctx.musicResponsiveListItemRenderer?.flexColumns[0]
?.musicResponsiveListItemFlexColumnRenderer.text?.runs[0]
?.navigationEndpoint?.watchEndpoint?.playerParams ||
ctx.musicResponsiveListItemRenderer?.flexColumns[0]
?.musicResponsiveListItemFlexColumnRenderer?.text?.runs[0]
?.navigationEndpoint?.watchEndpoint?.params
};
return Item;
};
};
suite.add("async function get(){", function () {
async function get(){
const _DATA = Promise.resolve(DATASET);
const DATA = await _DATA;
let len = DATA.length;
while (len--) {
DATA[len] = MusicResponsiveListItemRenderer(DATA[len]);
}
console.log(DATA)
}
get();
});
suite.on("cycle", function (evt) {
console.log(" - " + evt.target);
});
suite.on("complete", function (evt) {
console.log(new Array(30).join("-"));
var results = evt.currentTarget.sort(function (a, b) {
return b.hz - a.hz;
});
results.forEach(function (item) {
console.log((idx + 1) + ". " + item);
});
});
console.log("Untitled benchmark");
console.log(new Array(30).join("-"));
suite.run();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment