Bilibili wbi sign test - From mixin query to mixed params

Benchmark created on


Description

See also: https://socialsisteryi.github.io/bilibili-API-collect/docs/misc/sign/wbi.html

Setup

const targetParams = {
    a: '12345',
	b: '23456',
	c: '34567'
}
const extraParams = {
	d: '45678',
	e: '1234567890'
}
const extraParams2 = [
	'd=45678',
	'e=1234567890'
]
const reg = /[!'\(\)*]/g

Test runner

Ready to run.

Testing in
TestOps/sec
Official
var mixinQuery = Object.assign({}, targetParams, extraParams)
var sortedParams = Object.keys(mixinQuery).sort()
var charRegex = /[!'\(\)*]/g
var mixinParams = []
for (var i = 0; i < sortedParams.length; i++) {
	var key = sortedParams[i]
	var value = mixinQuery[key]

	if (value && typeof value === 'string') {
		value = value.replace(charRegex, '')
	}

	if (value != null) {
	mixinParams.push("".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(value)))
    }
}
var sortedParamsStr = mixinParams.join('&') // mixedParams
ready
Unofficial 1
const mixedQuery = {
    ...targetParams,
    ...extraParams,
}
const sortedParams = Object.keys(mixedQuery).sort()
const reg = /[!'\(\)*]/g
const mixedParams = []
for (let i = sortedParams.length - 1; i >= 0; --i) {
    const key = sortedParams[i]
    const value = mixedQuery[key]
    mixedParams[i] =
        key !== 'wts'
            ? `${encodeURIComponent(key)}=${encodeURIComponent(value.replace(reg, ''))}`
            : `${key}=${value}`
}
const result = mixedParams.join('&') // mixedParams
ready
Unofficial 2
const formattedTargetParams = []
const targetParamsKeys = Object.keys(targetParams)
for (let i = targetParamsKeys.length - 1; i >= 0; --i) {
    const key = targetParamsKeys[i]
    formattedTargetParams[i] = `${key}=${encodeURIComponent(targetParams[key].replace(reg, ''))}`
}
const mixedParams = formattedTargetParams.concat(extraParams2).sort().join('&')
ready
Unofficial 3
const formattedTargetParams = []
const targetParamsKeys = Object.keys(targetParams)
for (let i = targetParamsKeys.length - 1; i >= 0; --i) {
    const key = targetParamsKeys[i]
    formattedTargetParams.push(`${key}=${encodeURIComponent(
        targetParams[key].replace(reg, '')
    )}`)
}
const mixedParams = formattedTargetParams.concat(extraParams2).sort().join('&')
ready
Unofficial 4
const result = []
const targetParamsKeys = Object.keys(targetParams)
for (let i = targetParamsKeys.length - 1; i >= 0; --i) {
    const key = targetParamsKeys[i]
    result.push(`${key}=${encodeURIComponent(
        targetParams[key].replace(reg, '')
    )}`)
}
result.push(...extraParams2)
const mixedParams = result.sort().join('&')
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.