TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Show HN: Uint1Array – JavaScript Bit Array

24 点作者 19eightyfour将近 8 年前

3 条评论

Klathmon将近 8 年前
Damn I really wish I had this 6 months ago when I was re-implementing a way to convert ImageData into a 1-bit BMP image directly in the browser to easily interface with a legacy system.<p>Have you tested the performance of this? Some drop is expected over the larger size arrays, but are we talking magnitudes slower or just 2-3x slower?<p>I see most of the &quot;public&quot; methods are just using the `.toArray()` method of the private class, doesn&#x27;t that just murder the performance and negate the whole reason for using this?<p>We eventually basically implemented a version of this ourselves, but we ended up doing it a little differently. We would pull a full uint8 from the array and break it out into the 8 bits then just let the iterator return&#x2F;set those one at a time until it&#x27;s done, then it would write the whole 8 bits to the &quot;main&quot; array at once which massively sped things up for us.<p>Also, your getter to access the private section is adding a lot of overhead to accessing it. It&#x27;s probably better to just forego the &quot;absolute safety&quot; of being unable to overwrite your private inner class and just rely on the symbol to prevent access to the private bits.
评论 #14565605 未加载
cjhanks将近 8 年前
Ech, is that right? I don&#x27;t understand `wordSizeShift`.<p>I have always done bitset tests as follows:<p><pre><code> this.set = function(bit, value) { const index = Math.floor(bit &#x2F; BitsPerElement); const offset = bit - (index * BitsPerElement); var element = this.buffer[index]; if (value) element |= (1 &lt;&lt; offset); else element &amp;= ~(1 &lt;&lt; offset); this.buffer[index] = element; } this.get = function(bit) { const index = Math.floor(bit &#x2F; BitsPerElement); const offset = bit - (index * BitsPerElement); const element = this.buffer[index]; return (element &amp; (1 &lt;&lt; offset)); }</code></pre>
评论 #14565625 未加载
Klathmon将近 8 年前
There were a few comments you deleted before I could reply, so I just wanted to put this here.<p>I don&#x27;t mean any harm with my criticisms here, this was an area that I explored a lot with specific goals in mind and I was just trying to talk about why you went with the direction you did.<p>I didn&#x27;t mean any harm!