1.2.1130. Slice Arrays First

Always start by reducing an array before applying some transformation on it. The shorter array will be processed faster.

The gain produced here is greater with longer arrays, or greater reductions. They may also be used in loops. This is a micro-optimisation when used on short arrays.

<?php

// fast version
$a = array_map('foo', array_slice($array, 2, 5));

// slower version
$a = array_slice(array_map('foo', $array), 2, 5);
?>

1.2.1130.1. Suggestions

  • Use the array transforming function on the result of the array shortening function.

1.2.1130.2. Specs

Short name

Arrays/SliceFirst

Rulesets

All, Performances, Suggestions

Exakat since

1.0.4

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

array

Examples

WordPress

Available in

Entreprise Edition, Exakat Cloud