Skip to content

Sliding-window minimum

Find the leftmost smallest item in each full window of a sequence. Report the first minimum, then emit a new `(value, position)` pair only when its position changes. This private workload compares rescanning on expiry with a monotonic deque backed by either storage layout from `knotkit/deque`.

Find the leftmost smallest item in each full window of a sequence. Report the first minimum, then emit a new (value, position) pair only when its position changes. This private workload compares rescanning on expiry with a monotonic deque backed by either storage layout from knotkit/deque.

Increasing input at width 128: the array deque takes about 0.48 ms against 8.36 ms for rescanning; on random input rescanning is faster

  • knotkit/deque: Queues elements at both ends in O(1) amortized time, over array and Float64Array ring buffers, with an optional fixed capacity that turns the queue into a sliding window
{"v":1,"box":[103.1962,94.1597,874.5333,444.3555],"x":{"s":"log","lim":[1.55832915932,657.114059552],"inv":false,"g":0,"t":"Window width (items), 4,096 increasing values","tk":[[2,"2"],[4,"4"],[8,"8"],[16,"16"],[32,"32"],[64,"64"],[128,"128"],[256,"256"],[512,"512"]],"b":10},"y":{"s":"log","lim":[2925,2564709],"inv":false,"g":0,"tk":[[4096,"n = 4,096"],[10000,"10k"],[100000,"100k"],[1000000,"1M"],[1831935,"1.83M"]],"b":10},"ti":"Increasing input: rescan costs 447× the deque at width 512","a":[{"k":"line","c":"#fffff8","s":"x","d":[2,4095,4,12279,8,28623,16,61215,32,126015,64,254079,128,504063,256,979455,512,1831935],"p":[135.04,426.97,223.5,370.22,311.95,326.48,400.41,287.19,488.86,249.88,577.32,213.64,665.78,178.24,754.23,143.91,842.69,111.55]},{"k":"line","c":"#c4501b","s":"x","d":[2,4095,4,12279,8,28623,16,61215,32,126015,64,254079,128,504063,256,979455,512,1831935],"p":[135.04,426.97,223.5,370.22,311.95,326.48,400.41,287.19,488.86,249.88,577.32,213.64,665.78,178.24,754.23,143.91,842.69,111.55]},{"k":"line","c":"#fffff8","s":"x","d":[2,4095,4,4095,8,4095,16,4095,32,4095,64,4095,128,4095,256,4095,512,4095],"p":[135.04,426.97,223.5,426.97,311.95,426.97,400.41,426.97,488.86,426.97,577.32,426.97,665.78,426.97,754.23,426.97,842.69,426.97]},{"k":"line","c":"#1d5fa6","s":"x","d":[2,4095,4,4095,8,4095,16,4095,32,4095,64,4095,128,4095,256,4095,512,4095],"p":[135.04,426.97,223.5,426.97,311.95,426.97,400.41,426.97,488.86,426.97,577.32,426.97,665.78,426.97,754.23,426.97,842.69,426.97]},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"line","c":"#8c8c84"},{"k":"annotation","c":"#5c5c57"},{"k":"annotation","c":"#c4501b"},{"k":"annotation","c":"#1d5fa6"}],"lg":[]} 2 4 8 16 32 64 128 256 512 Window width (items), 4,096 increasing values n = 4,096 10k 100k 1M 1.83M Comparator calls Rescan on expiry Monotonic deque Increasing input: rescan costs 447× the deque at width 512 {"v":1,"box":[103.1962,605.1597,874.5333,955.3555],"x":{"s":"log","lim":[1.55832915932,657.114059552],"inv":false,"g":1,"t":"Window width (items), 4,096 seeded random values","tk":[[2,"2"],[4,"4"],[8,"8"],[16,"16"],[32,"32"],[64,"64"],[128,"128"],[256,"256"],[512,"512"]],"b":10},"y":{"s":"log","lim":[2925,2564709],"inv":false,"g":1,"tk":[[4096,"n = 4,096"],[10000,"10k"],[100000,"100k"],[1000000,"1M"],[1831935,"1.83M"]],"b":10},"ti":"Random input: rescan costs 1.2× the deque at width 512","a":[{"k":"line","c":"#fffff8","s":"x","d":[2,4095,4,6155,8,7203,16,7749,32,7845,64,8311,128,8127,256,8667,512,9705],"p":[135.04,937.97,223.5,916.91,311.95,908.78,400.41,905.01,488.86,904.37,577.32,901.39,665.78,902.54,754.23,899.22,842.69,893.37]},{"k":"line","c":"#c4501b","s":"x","d":[2,4095,4,6155,8,7203,16,7749,32,7845,64,8311,128,8127,256,8667,512,9705],"p":[135.04,937.97,223.5,916.91,311.95,908.78,400.41,905.01,488.86,904.37,577.32,901.39,665.78,902.54,754.23,899.22,842.69,893.37]},{"k":"line","c":"#fffff8","s":"x","d":[2,4095,4,6147,8,7146,16,7676,32,7935,64,8051,128,8119,256,8149,512,8164],"p":[135.04,937.97,223.5,916.97,311.95,909.19,400.41,905.49,488.86,903.78,577.32,903.03,665.78,902.6,754.23,902.4,842.69,902.31]},{"k":"line","c":"#1d5fa6","s":"x","d":[2,4095,4,6147,8,7146,16,7676,32,7935,64,8051,128,8119,256,8149,512,8164],"p":[135.04,937.97,223.5,916.97,311.95,909.19,400.41,905.49,488.86,903.78,577.32,903.03,665.78,902.6,754.23,902.4,842.69,902.31]},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"line","c":"#8c8c84"},{"k":"annotation","c":"#5c5c57"},{"k":"annotation","c":"#c4501b"},{"k":"annotation","c":"#1d5fa6"}],"lg":[]} 2 4 8 16 32 64 128 256 512 Window width (items), 4,096 seeded random values n = 4,096 10k 100k 1M 1.83M Comparator calls Rescan on expiry Monotonic deque Random input: rescan costs 1.2× the deque at width 512
{"v":1,"box":[103.1962,122.3921,426.5333,444.3555],"x":{"s":"log","lim":[1.55832915932,657.114059552],"inv":false,"g":0,"t":"Window width (items), 4,096 increasing values","tk":[[2,"2"],[4,"4"],[8,"8"],[16,"16"],[32,"32"],[64,"64"],[128,"128"],[256,"256"],[512,"512"]],"b":10},"y":{"s":"log","lim":[2925,2564709],"inv":false,"g":0,"tk":[[4096,"n = 4,096"],[10000,"10k"],[100000,"100k"],[1000000,"1M"],[1831935,"1.83M"]],"b":10},"ti":"Increasing input: rescan costs 447× the\ndeque at width 512","a":[{"k":"line","c":"#fffff8","s":"x","d":[2,4095,4,12279,8,28623,16,61215,32,126015,64,254079,128,504063,256,979455,512,1831935],"p":[116.54,428.37,153.62,376.19,190.7,335.98,227.78,299.86,264.86,265.56,301.94,232.24,339.02,199.69,376.1,168.13,413.18,138.38]},{"k":"line","c":"#c4501b","s":"x","d":[2,4095,4,12279,8,28623,16,61215,32,126015,64,254079,128,504063,256,979455,512,1831935],"p":[116.54,428.37,153.62,376.19,190.7,335.98,227.78,299.86,264.86,265.56,301.94,232.24,339.02,199.69,376.1,168.13,413.18,138.38]},{"k":"line","c":"#fffff8","s":"x","d":[2,4095,4,4095,8,4095,16,4095,32,4095,64,4095,128,4095,256,4095,512,4095],"p":[116.54,428.37,153.62,428.37,190.7,428.37,227.78,428.37,264.86,428.37,301.94,428.37,339.02,428.37,376.1,428.37,413.18,428.37]},{"k":"line","c":"#1d5fa6","s":"x","d":[2,4095,4,4095,8,4095,16,4095,32,4095,64,4095,128,4095,256,4095,512,4095],"p":[116.54,428.37,153.62,428.37,190.7,428.37,227.78,428.37,264.86,428.37,301.94,428.37,339.02,428.37,376.1,428.37,413.18,428.37]},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"line","c":"#8c8c84"},{"k":"annotation","c":"#5c5c57"},{"k":"annotation","c":"#c4501b"},{"k":"annotation","c":"#1d5fa6"}],"lg":[]} 2 4 8 16 32 64 128 256 512 Window width (items), 4,096 increasing values n = 4,096 10k 100k 1M 1.83M Comparator calls Rescan on expiry Monotonic deque Increasing input: rescan costs 447× the deque at width 512 {"v":1,"box":[103.1962,633.3921,426.5333,955.3555],"x":{"s":"log","lim":[1.55832915932,657.114059552],"inv":false,"g":1,"t":"Window width (items), 4,096 seeded random values","tk":[[2,"2"],[4,"4"],[8,"8"],[16,"16"],[32,"32"],[64,"64"],[128,"128"],[256,"256"],[512,"512"]],"b":10},"y":{"s":"log","lim":[2925,2564709],"inv":false,"g":1,"tk":[[4096,"n = 4,096"],[10000,"10k"],[100000,"100k"],[1000000,"1M"],[1831935,"1.83M"]],"b":10},"ti":"Random input: rescan costs 1.2× the deque\nat width 512","a":[{"k":"line","c":"#fffff8","s":"x","d":[2,4095,4,6155,8,7203,16,7749,32,7845,64,8311,128,8127,256,8667,512,9705],"p":[116.54,939.37,153.62,920.01,190.7,912.54,227.78,909.06,264.86,908.48,301.94,905.74,339.02,906.8,376.1,903.75,413.18,898.37]},{"k":"line","c":"#c4501b","s":"x","d":[2,4095,4,6155,8,7203,16,7749,32,7845,64,8311,128,8127,256,8667,512,9705],"p":[116.54,939.37,153.62,920.01,190.7,912.54,227.78,909.06,264.86,908.48,301.94,905.74,339.02,906.8,376.1,903.75,413.18,898.37]},{"k":"line","c":"#fffff8","s":"x","d":[2,4095,4,6147,8,7146,16,7676,32,7935,64,8051,128,8119,256,8149,512,8164],"p":[116.54,939.37,153.62,920.07,190.7,912.91,227.78,909.51,264.86,907.94,301.94,907.25,339.02,906.85,376.1,906.67,413.18,906.59]},{"k":"line","c":"#1d5fa6","s":"x","d":[2,4095,4,6147,8,7146,16,7676,32,7935,64,8051,128,8119,256,8149,512,8164],"p":[116.54,939.37,153.62,920.07,190.7,912.91,227.78,909.51,264.86,907.94,301.94,907.25,339.02,906.85,376.1,906.67,413.18,906.59]},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"line","c":"#8c8c84"},{"k":"annotation","c":"#5c5c57"},{"k":"annotation","c":"#c4501b"},{"k":"annotation","c":"#1d5fa6"}],"lg":[]} 2 4 8 16 32 64 128 256 512 Window width (items), 4,096 seeded random values n = 4,096 10k 100k 1M 1.83M Comparator calls Rescan on expiry Monotonic deque Random input: rescan costs 1.2× the deque at width 512
  • The minimum expires at every step on this increasing sequence.
  • At width 512: 1831935 rescan comparisons and 4095 deque comparisons.
  • On seeded random input at width 512: 9705 rescan comparisons and 8164 deque comparisons.
  • The deque uses bounded candidate storage to avoid rescanning a whole window.

Hover or focus a chart for exact values. Drawn by demo.ts at build time, from the fixtures the benchmark uses; it reproduces the README figure.

Terminal window
git clone https://github.com/jkomyno/knotkit.git && cd knotkit && bun install
bun run demo:problem:sliding-window-minimum

problems/sliding-window-minimum/README.md states the fixture, the seed, and the result table.