# maxFittingPieces

> **maxFittingPieces**(`options`): `number`

Defined in: [packages/synapse-core/src/sp/add-pieces-fits.ts:164](https://github.com/FilOzone/synapse-sdk/blob/56f1ef3fb5ccfea19fb80cb59e997e381f37d85a/packages/synapse-core/src/sp/add-pieces-fits.ts#L164)

Length of the longest prefix of `options.pieces` that fits in one addPieces / createAndAdd message.

Equivalent to the largest `n` for which [addPiecesFits](/reference/filoz/synapse-core/sp/functions/addpiecesfits/) returns `true` on `pieces.slice(0, n)`,
or `0` if none fit. Stops before the first piece with invalid metadata. Binary searches the prefix
length, encoding the calldata O(log n) times instead of once per piece. Never throws.

## Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `options` | [`LimiterOptions`](/reference/filoz/synapse-core/sp/type-aliases/limiteroptions/) | [maxFittingPieces.OptionsType](/reference/filoz/synapse-core/sp/namespaces/maxfittingpieces/type-aliases/optionstype/) |

## Returns

`number`

Number of leading pieces that fit [maxFittingPieces.OutputType](/reference/filoz/synapse-core/sp/namespaces/maxfittingpieces/type-aliases/outputtype/)

## Example

```ts
import { maxFittingPieces } from '@filoz/synapse-core/sp'

const count = maxFittingPieces({ kind: 'addPieces', dataSetId, legacyPieceStorageIdLimit, pieces })
const batch = pieces.slice(0, count)
```