Skip to content
中文

commonPrefixLength ​

CORE · function · Runtime value

Length of the longest common prefix of two strings.

Implemented as a binary search over slice comparisons so long prompts stay on the native string comparison path instead of running a JavaScript character loop.

Import ​

ts
import { commonPrefixLength } from '@jangts/agent-core/context';
js
import { commonPrefixLength } from '@jangts/agent-core/context';

Other public entry points:@jangts/agent-core, @jangts/agent-core/types, @jangts/agent-core/context, @jangts/agent-core/support, @jangts/agent-core/support/context.

Definition ​

Call ​

ts
commonPrefixLength(left: string, right: string): number

Length of the longest common prefix of two strings.

Implemented as a binary search over slice comparisons so long prompts stay on the native string comparison path instead of running a JavaScript character loop.

ParameterTypePassingDescription
leftstringRequired—
rightstringRequired—

Returns: number.

Examples ​

Compare the stable prefix of two real inputs ​

Beginner · Node.js

ts
import { commonPrefixLength } from '@jangts/agent-core';
console.log(commonPrefixLength('服务:正常', '服务:繁忙')); // 3
js
import { commonPrefixLength } from '@jangts/agent-core';
console.log(commonPrefixLength('服务:正常', '服务:繁忙')); // 3

Different object key order keeps the same structure semantics ​

Configuration · Node.js

ts
import { retrievalQueryKey } from '@jangts/agent-core';
const first = retrievalQueryKey({ query: '服务状态', filters: { region: 'cn', active: true } });
const second = retrievalQueryKey({ filters: { active: true, region: 'cn' }, query: '服务状态' });
console.log(first === second); // true
console.log(retrievalQueryKey({ ids: ['a', 'b'] }) === retrievalQueryKey({ ids: ['b', 'a'] })); // false
js
import { retrievalQueryKey } from '@jangts/agent-core';
const first = retrievalQueryKey({ query: '服务状态', filters: { region: 'cn', active: true } });
const second = retrievalQueryKey({ filters: { active: true, region: 'cn' }, query: '服务状态' });
console.log(first === second); // true
console.log(retrievalQueryKey({ ids: ['a', 'b'] }) === retrievalQueryKey({ ids: ['b', 'a'] })); // false

API reference, usage examples and developer guides.