Skip to content

Commit c24ac3d

Browse files
Cache the count function context in constructor
1 parent 4e3da12 commit c24ac3d

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

packages/govuk-frontend/src/govuk/components/character-count/character-count.mjs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ export class CharacterCount extends ConfigurableComponent {
3131
/** @private */
3232
countFunction
3333

34+
/** @private */
35+
countFunctionContext
36+
3437
/**
3538
* @private
3639
* @type {Intl.Segmenter | null}
@@ -160,6 +163,12 @@ export class CharacterCount extends ConfigurableComponent {
160163
this.config.countFunction ??
161164
this.countFunctions[this.config.countType ?? 'length']
162165

166+
// Cache the count function context
167+
this.countFunctionContext = {
168+
config: this.config,
169+
segmenter: this.segmenter
170+
}
171+
163172
// Determine the limit attribute (characters or words)
164173
this.maxLength = this.config.maxlength ?? Infinity
165174

@@ -392,12 +401,10 @@ export class CharacterCount extends ConfigurableComponent {
392401
* @private
393402
*/
394403
updateCount() {
395-
const text = this.$textarea.value
396-
397-
this.count = this.countFunction(text, {
398-
config: this.config,
399-
segmenter: this.segmenter
400-
})
404+
this.count = this.countFunction(
405+
this.$textarea.value,
406+
this.countFunctionContext
407+
)
401408
}
402409

403410
/**

0 commit comments

Comments
 (0)