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

solution and test optimization throttle #90

Open
wants to merge 2 commits into
base: gh-pages
Choose a base branch
from
Open
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
50 changes: 33 additions & 17 deletions exercise/throttle.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,60 +20,76 @@ export const tests = []
const t = (f) => tests.push(f)

const add = (arr, el) => arr.push(el)
const run = (callback, callLimit, nbr) =>
const run = (callback, { delay, count }) =>
new Promise((r) => {
let arr = []
let inter = setInterval(() => callback(arr, 1), callLimit)
let inter = setInterval(() => callback(arr, 1), delay)
setTimeout(() => {
clearInterval(inter)
r(arr.length)
}, callLimit * nbr)
}, delay * count)
})

// wait 26ms and execute 4 times every 16ms, executes with a wait time of 26
t(async ({ eq }) => eq(await run(throttle(add, 26), 16, 4), 2))
t(async ({ eq }) =>
eq(await run(throttle(add, 26), { delay: 16, count: 4 }), 2)
)

// wait 20ms and execute 2 times every 10ms, executes with a wait time of 26
t(async ({ eq }) => eq(await run(throttle(add, 20), 10, 2), 1))
t(async ({ eq }) =>
eq(await run(throttle(add, 20), { delay: 10, count: 2 }), 1)
)

// wait 16ms and execute 5 times every 26ms, will execute with out waiting
t(async ({ eq }) => eq(await run(throttle(add, 16), 26, 5), 4))
t(async ({ eq }) =>
eq(await run(throttle(add, 16), { delay: 26, count: 5 }), 4)
)

// it works concurently
t(async ({ eq }) =>
eq(
await Promise.all([
run(throttle(add, 16), 26, 5),
run(throttle(add, 16), 26, 5),
run(throttle(add, 16), { delay: 26, count: 5 }),
run(throttle(add, 16), { delay: 26, count: 5 }),
]),
[4, 4]
)
)

// tests the trailing option
t(async ({ eq }) =>
eq(await run(opThrottle(add, 26, { trailing: true }), 16, 4), 1)
eq(
await run(opThrottle(add, 26, { trailing: true }), { delay: 16, count: 4 }),
1
)
)

// tests the leading option with wait time in the leading edge of the timeout
t(async ({ eq }) =>
eq(await run(opThrottle(add, 15, { leading: true }), 10, 10), 5)
)

// tests the leading option with wait time not in the leading edge of the timeout
t(async ({ eq }) =>
eq(await run(opThrottle(add, 26, { leading: true }), 16, 4), 2)
eq(
await Promise.all([
run(opThrottle(add, 15, { leading: true }), { delay: 10, count: 10 }),
run(opThrottle(add, 26, { leading: true }), { delay: 16, count: 4 }),
]),
[5, 2]
)
)

// tests without options
t(async ({ eq }) => eq(await run(opThrottle(add, 10), 5, 2), 0))
t(async ({ eq }) =>
eq(await run(opThrottle(add, 10), { delay: 5, count: 2 }), 0)
)

// tests with both options true
t(async ({ eq }) =>
eq(
await run(opThrottle(add, 26, { trailing: true, leading: true }), 16, 4),
await run(opThrottle(add, 26, { trailing: true, leading: true }), {
delay: 16,
count: 4,
}),
2
)
)

Object.freeze(tests)
Object.freeze(tests)
5 changes: 3 additions & 2 deletions tests/throttle/pass_lee.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let timeOut = 0
const throttle = (callback, limit) => {
let timeOut = 0
return (...args) => {
if (!timeOut) {
timeOut = setTimeout(() => (timeOut = 0), limit)
Expand All @@ -9,11 +9,12 @@ const throttle = (callback, limit) => {
}

const opThrottle = (callback, waitLimit, options = {}) => {
let timeOut = 0
let result
let beginning = 0
return (...args) => {
let now = Date.now()
if (!beginning && options.leading === false) beginning = now
if (!beginning && (options.leading === false || !options.leading)) beginning = now
let remaining = waitLimit - (now - beginning)
if (remaining <= 0 || remaining > waitLimit) {
beginning = now
Expand Down