Base64 icon indicating copy to clipboard operation
Base64 copied to clipboard

Improve performance.

Open howmanysmall opened this issue 4 years ago • 2 comments

This version runs significantly better (it's the older version of the library with a few tweaks).

local Base64 = require(script.Base64)
local HmsBase64 = require(script.HmsBase64)

local function BenchmarkFunction(Library)
	return function(Profiler, String)
		Profiler.Begin("Encode")
		local New = Library.encode(String)
		Profiler.End()

		Profiler.Begin("Decode")
		local _ = Library.decode(New)
		Profiler.End()
	end
end

local CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`~!@#$%^&*()-_=+[{]}\\|;:'\"<,>./?"
local RandomLib = Random.new(os.clock() % 1 * 1E7)

local LENGTH = 1000

local function RandomString()
	local String = table.create(LENGTH)
	local Length = 0

	for _ = LENGTH, 0, -1 do
		local Index = math.floor(RandomLib:NextNumber() * 94)
		Length += 1
		String[Length] = string.sub(CHARACTERS, Index, Index)
	end

	return table.concat(String)
end

local String = RandomString()
assert(Base64.encode(String) == HmsBase64.encode(String))

return {
	ParameterGenerator = RandomString;

	Functions = {
		["HmsBase64"] = BenchmarkFunction(HmsBase64);
		["Base64"] = BenchmarkFunction(Base64);
	};
}

This code nets the following result: image

howmanysmall avatar Nov 03 '21 19:11 howmanysmall

This needs to be tested with different input sizes. I found the latest version was up to 10x faster when I tested it on large payloads (1mb+).

Reselim avatar Nov 03 '21 22:11 Reselim

image I got the opposite with a payload of about 1mb.

image As well as 3.3mb.

Here's the code in zip file. Base64.zip

howmanysmall avatar Nov 03 '21 23:11 howmanysmall