Skip to content

Commit e3087a5

Browse files
committed
Remove idempotent property of encoding
The idea was that, if we knew that encode() and decode() were idempotent, we could in certain situations skip a decode step in abstract-leveldown and avoid allocating a callback function. For example if leveldown returned a string to be decoded with the utf8 encoding; that would be a noop we could skip. It worked fine. But I didn't get to test that it had a significant performance benefit. And to avoid painting ourselves into a corner with this optimization (for example, if we need to normalize data in decode() like level-js does) I'm not gonna spend more time on it.
1 parent 1f40104 commit e3087a5

File tree

3 files changed

+6
-25
lines changed

3 files changed

+6
-25
lines changed

lib/encoding.d.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@ export abstract class Encoding<TIn, TFormat, TOut> {
1616
/** Unique name for this encoding. */
1717
type: string
1818

19-
/** Format returned by {@link encode}. */
20-
format: string
21-
2219
/**
23-
* Whether {@link encode} and {@link decode} are idempotent
24-
* functions, such that `f(x) == f(f(x))`.
20+
* Indicates the (lower-level) encoding used by the return value
21+
* of {@link encode}. Typically one of 'buffer', 'view', 'utf8'.
2522
*/
26-
idempotent: boolean
23+
format: string
2724

2825
createViewTranscoder (): ViewFormat<TIn, TOut> | undefined
2926
createBufferTranscoder (): BufferFormat<TIn, TOut> | undefined
@@ -53,7 +50,8 @@ export interface EncodingOptions<TIn, TFormat, TOut> {
5350
type?: string | undefined
5451

5552
/**
56-
* Format returned by {@link encode}.
53+
* Indicates the (lower-level) encoding used by the return value
54+
* of {@link encode}. Typically one of 'buffer', 'view', 'utf8'.
5755
*/
5856
format?: string | undefined
5957

lib/encoding.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ class Encoding {
4747
/** @type {string} */
4848
this.format = format || (buffer === false ? 'utf8' : 'buffer')
4949

50-
/** @type {boolean} */
51-
this.idempotent = this.type === this.format
52-
5350
if (createViewTranscoder) {
5451
this.createViewTranscoder = createViewTranscoder
5552
}

test/transcoder.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,10 @@ test('transcoder.encoding() wraps custom anonymous encoding', function (t) {
182182
const transcoder = new Transcoder(['buffer', 'view', 'utf8'])
183183
const spy = (v) => v + 1
184184
const opts = { encode: spy, decode: spy }
185-
const verify = (encoding, idempotent) => {
185+
const verify = (encoding) => {
186186
t.is(encoding.encode(1), 2, 'has encode() function')
187187
t.is(encoding.decode(1), 2, 'has decode() function')
188188
t.ok(/^anonymous-\d+$/.test(encoding.type), 'is anonymous: ' + encoding.type)
189-
t.is(encoding.idempotent, idempotent, 'idempotent: ' + idempotent)
190189
t.is(encoding.buffer, undefined, 'does not expose legacy buffer option')
191190
}
192191

@@ -216,16 +215,3 @@ test('transcoder.encoding() wraps custom anonymous encoding', function (t) {
216215

217216
t.end()
218217
})
219-
220-
test('has idempotent properties', function (t) {
221-
const transcoder = new Transcoder(['buffer', 'view', 'utf8', 'id'])
222-
223-
t.is(transcoder.encoding('buffer').idempotent, true)
224-
t.is(transcoder.encoding('view').idempotent, true)
225-
t.is(transcoder.encoding('utf8').idempotent, true)
226-
t.is(transcoder.encoding('id').idempotent, true)
227-
t.is(transcoder.encoding('json').idempotent, false)
228-
t.is(transcoder.encoding('base64').idempotent, false)
229-
t.is(transcoder.encoding('hex').idempotent, false)
230-
t.end()
231-
})

0 commit comments

Comments
 (0)