3v4l.org

run code in 300+ PHP versions simultaneously
<?php /**_______________________________________ * * FastJSON, * simple and fast Pear Service_JSON encoder/decoder alternative * [http://pear.php.net/pepr/pepr-proposal-show.php?id=198] * --------------------------------------- * This class is about two time faster than Pear Service_JSON class. * This class is probably not powerful as Service_JSON but it has * no dependencies and converts correctly ASCII range 0x00 - 0x1F too. * There's any string convertion, just regular RFC specific characters are converted * into \u00XX string. * To don't have problems with other chars try to use utf8_encode($json_encoded_string). * To recieve correctly JSON strings from JavaScript use encodeURIComponent then * use, if is necessary, utef8_decode before JS to PHP convertion. * decode method doesn't returns a standard object class but You can * create the corret class directly with FastJSON::convert method * and with them You can manage JS Date objects too. * --------------------------------------- * Summary of static public methods * * convert * extra, special method * * decode * converts a valid JSON string * into a native PHP variable * * encode * converts a native php variable * into a valid JSON string * --------------------------------------- * * Special FastJSON::convert method Informations * _______________________________________ * --------------------------------------- * This method is used by FastJSON::encode method but should be used * to do these convertions too: * * - JSON string to time() integer: * * FastJSON::convert(decodedDate:String):time() * * If You recieve a date string rappresentation You * could convert into respective time() integer. * Example: * FastJSON::convert(FastJSON::decode($clienttime)); * // i.e. $clienttime = 2006-11-09T14:42:30 * // returned time will be an integer useful with gmdate or date * // to create, for example, this string * // Thu Nov 09 2006 14:42:30 GMT+0100 (Rome, Europe) * * - time() to JSON string: * * FastJSON::convert(time():Int32, true:Boolean):JSON Date String format * * You could send server time() informations and send them to clients. * Example: * FastJSON::convert(time(), true); * // i.e. 2006-11-09T14:42:30 * * - associative array to generic class: * * FastJSON::convert(array(params=>values), new GenericClass):new Instance of GenericClass * * With a decoded JSON object You could convert them * into a new instance of your Generic Class. * Example: * class MyClass { * var $param = "somevalue"; * function MyClass($somevar) { * $this->somevar = $somevar; * }; * function getVar = function(){ * return $this->somevar; * }; * }; * * $instance = new MyClass("example"); * $encoded = FastJSON::encode($instance); * // {"param":"somevalue"} * * $decoded = FastJSON::decode($encoded); * // $decoded instanceof Object => true * // $decoded instanceof MyClass => false * * $decoded = FastJSON::convert($decoded, new MyClass("example")); * // $decoded instanceof Object => true * // $decoded instanceof MyClass => true * * $decoded->getVar(); // example * * --------------------------------------- * * @author Andrea Giammarchi * @site http://www.devpro.it/ * @version 0.4 [fixed string convertion problems, add stdClass optional convertion instead of associative array (used by default)] * @requires anything * @compatibility PHP >= 4 * @license * --------------------------------------- * * Copyright (c) 2006 - 2007 Andrea Giammarchi * * Permission is hereby granted, free of charge, * to any person obtaining a copy of this software and associated * documentation files (the "Software"), * to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, sublicense, and/or sell copies of the Software, * and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * _______________________________________ */ class FastJSON { // public methods /** * public static method * * FastJSON::convert(params:* [, result:Instance]):* * * @param * String or Object * @param Instance optional new generic class instance if first * parameter is an object. * @return * time() value or new Instance with object parameters. * * @note please read Special FastJSON::convert method Informations */ function convert($params, $result = null){ switch(gettype($params)){ case 'array': $tmp = array(); foreach($params as $key => $value) { if(($value = FastJSON::encode($value)) !== '') array_push($tmp, FastJSON::encode(strval($key)).':'.$value); }; $result = '{'.implode(',', $tmp).'}'; break; case 'boolean': $result = $params ? 'true' : 'false'; break; case 'double': case 'float': case 'integer': $result = $result !== null ? strftime('%Y-%m-%dT%H:%M:%S', $params) : strval($params); break; case 'NULL': $result = 'null'; break; case 'string': $i = create_function('&$e, $p, $l', 'return intval(substr($e, $p, $l));'); if(preg_match('/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$/', $params)) $result = mktime($i($params, 11, 2), $i($params, 14, 2), $i($params, 17, 2), $i($params, 5, 2), $i($params, 9, 2), $i($params, 0, 4)); break; case 'object': $tmp = array(); if(is_object($result)) { foreach($params as $key => $value) $result->$key = $value; } else { $result = get_object_vars($params); foreach($result as $key => $value) { if(($value = FastJSON::encode($value)) !== '') array_push($tmp, FastJSON::encode($key).':'.$value); }; $result = '{'.implode(',', $tmp).'}'; } break; } return $result; } /** * public method * * FastJSON::decode(params:String[, useStdClass:Boolean]):* * * @param String valid JSON encoded string * @param Bolean uses stdClass instead of associative array if params contains objects (default false) * @return * converted variable or null * is params is not a JSON compatible string. * @note This method works in an optimist way. If JSON string is not valid * the code execution will die using exit. * This is probably not so good but JSON is often used combined with * XMLHttpRequest then I suppose that's better more protection than * just some WARNING. * With every kind of valid JSON string the old error_reporting level * and the old error_handler will be restored. * * @example * FastJSON::decode('{"param":"value"}'); // associative array * FastJSON::decode('{"param":"value"}', true); // stdClass * FastJSON::decode('["one",two,true,false,null,{},[1,2]]'); // array */ function decode($encode, $stdClass = false){ $pos = 0; $slen = is_string($encode) ? strlen($encode) : null; if($slen !== null) { $error = error_reporting(0); set_error_handler(array('FastJSON', '__exit')); $result = FastJSON::__decode($encode, $pos, $slen, $stdClass); error_reporting($error); restore_error_handler(); } else $result = null; return $result; } /** * public method * * FastJSON::encode(params:*):String * * @param * Array, Boolean, Float, Int, Object, String or NULL variable. * @return String JSON genric object rappresentation * or empty string if param is not compatible. * * @example * FastJSON::encode(array(1,"two")); // '[1,"two"]' * * $obj = new MyClass(); * obj->param = "value"; * obj->param2 = "value2"; * FastJSON::encode(obj); // '{"param":"value","param2":"value2"}' */ function encode($decode){ $result = ''; switch(gettype($decode)){ case 'array': if(!count($decode) || array_keys($decode) === range(0, count($decode) - 1)) { $keys = array(); foreach($decode as $value) { if(($value = FastJSON::encode($value)) !== '') array_push($keys, $value); } $result = '['.implode(',', $keys).']'; } else $result = FastJSON::convert($decode); break; case 'string': $replacement = FastJSON::__getStaticReplacement(); $result = '"'.str_replace($replacement['find'], $replacement['replace'], $decode).'"'; break; default: if(!is_callable($decode)) $result = FastJSON::convert($decode); break; } return $result; } // private methods, uncommented, sorry function __getStaticReplacement(){ static $replacement = array('find'=>array(), 'replace'=>array()); if($replacement['find'] == array()) { foreach(array_merge(range(0, 7), array(11), range(14, 31)) as $v) { $replacement['find'][] = chr($v); $replacement['replace'][] = "\\u00".sprintf("%02x", $v); } $replacement['find'] = array_merge(array(chr(0x5c), chr(0x2F), chr(0x22), chr(0x0d), chr(0x0c), chr(0x0a), chr(0x09), chr(0x08)), $replacement['find']); $replacement['replace'] = array_merge(array('\\\\', '\\/', '\\"', '\r', '\f', '\n', '\t', '\b'), $replacement['replace']); } return $replacement; } function __decode(&$encode, &$pos, &$slen, &$stdClass){ switch($encode{$pos}) { case 't': $result = true; $pos += 4; break; case 'f': $result = false; $pos += 5; break; case 'n': $result = null; $pos += 4; break; case '[': $result = array(); ++$pos; while($encode{$pos} !== ']') { array_push($result, FastJSON::__decode($encode, $pos, $slen, $stdClass)); if($encode{$pos} === ',') ++$pos; } ++$pos; break; case '{': $result = $stdClass ? new stdClass : array(); ++$pos; while($encode{$pos} !== '}') { $tmp = FastJSON::__decodeString($encode, $pos); ++$pos; if($stdClass) $result->$tmp = FastJSON::__decode($encode, $pos, $slen, $stdClass); else $result[$tmp] = FastJSON::__decode($encode, $pos, $slen, $stdClass); if($encode{$pos} === ',') ++$pos; } ++$pos; break; case '"': switch($encode{++$pos}) { case '"': $result = ""; break; default: $result = FastJSON::__decodeString($encode, $pos); break; } ++$pos; break; default: $tmp = ''; preg_replace('/^(\-)?([0-9]+)(\.[0-9]+)?([eE]\+[0-9]+)?/e', '$tmp = "\\1\\2\\3\\4"', substr($encode, $pos)); if($tmp !== '') { $pos += strlen($tmp); $nint = intval($tmp); $nfloat = floatval($tmp); $result = $nfloat == $nint ? $nint : $nfloat; } break; } return $result; } function __decodeString(&$encode, &$pos) { $replacement = FastJSON::__getStaticReplacement(); $endString = FastJSON::__endString($encode, $pos, $pos); $result = str_replace($replacement['replace'], $replacement['find'], substr($encode, $pos, $endString)); $pos += $endString; return $result; } function __endString(&$encode, $position, &$pos) { do { $position = strpos($encode, '"', $position + 1); }while($position !== false && FastJSON::__slashedChar($encode, $position - 1)); if($position === false) trigger_error('', E_USER_WARNING); return $position - $pos; } function __exit($str, $a, $b) { exit($a.'FATAL: FastJSON decode method failure [malicious or incorrect JSON string]'); } function __slashedChar(&$encode, $position) { $pos = 0; while($encode{$position--} === '\\') $pos++; return $pos % 2; } } ?>

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0150.00618.30
8.3.50.0090.00921.91
8.3.40.0040.01218.79
8.3.30.0090.00619.09
8.3.20.0050.00320.16
8.3.10.0050.00321.80
8.3.00.0080.00020.64
8.2.180.0160.00616.50
8.2.170.0140.00722.96
8.2.160.0100.00322.16
8.2.150.0040.00424.18
8.2.140.0080.00024.66
8.2.130.0050.00326.16
8.2.120.0030.00621.93
8.2.110.0080.00322.26
8.2.100.0030.01017.72
8.2.90.0040.00419.09
8.2.80.0030.00617.97
8.2.70.0090.00017.50
8.2.60.0030.00617.80
8.2.50.0080.00018.07
8.2.40.0040.00418.22
8.2.30.0000.00817.95
8.2.20.0000.00817.54
8.2.10.0000.00819.25
8.2.00.0000.00817.65
8.1.280.0070.00725.92
8.1.270.0090.00023.87
8.1.260.0080.00026.35
8.1.250.0080.00028.09
8.1.240.0030.00623.84
8.1.230.0040.00717.45
8.1.220.0050.00317.74
8.1.210.0080.00018.77
8.1.200.0000.01017.22
8.1.190.0040.00417.41
8.1.180.0000.00818.10
8.1.170.0000.00818.44
8.1.160.0000.00822.05
8.1.150.0040.00418.52
8.1.140.0040.00417.42
8.1.130.0000.00717.85
8.1.120.0020.00517.38
8.1.110.0080.00017.39
8.1.100.0070.00017.46
8.1.90.0000.00717.41
8.1.80.0070.00017.36
8.1.70.0030.00317.42
8.1.60.0040.00417.55
8.1.50.0030.00517.51
8.1.40.0000.00817.39
8.1.30.0000.00817.54
8.1.20.0040.00417.53
8.1.10.0040.00417.30
8.1.00.0040.00417.27
8.0.300.0040.00418.77
8.0.290.0080.00016.63
8.0.280.0000.00718.45
8.0.270.0030.00317.21
8.0.260.0050.00318.48
8.0.250.0000.00716.98
8.0.240.0060.00317.02
8.0.230.0030.00316.99
8.0.220.0050.00216.96
8.0.210.0000.00716.81
8.0.200.0000.00717.00
8.0.190.0000.00816.98
8.0.180.0070.00316.95
8.0.170.0040.00416.94
8.0.160.0030.00616.79
8.0.150.0000.00816.92
8.0.140.0050.00316.87
8.0.130.0000.00613.31
8.0.120.0040.00416.88
8.0.110.0040.00416.82
8.0.100.0000.00816.87
8.0.90.0080.00016.96
8.0.80.0090.00616.95
8.0.70.0030.00616.92
8.0.60.0080.00016.95
8.0.50.0000.00816.91
8.0.30.0100.01316.95
8.0.20.0070.01117.40
8.0.10.0030.00616.95
8.0.00.0140.00516.85
7.4.330.0030.00315.00
7.4.320.0030.00316.59
7.4.300.0030.00316.37
7.4.290.0050.00316.56
7.4.280.0030.00716.47
7.4.270.0000.00716.38
7.4.260.0000.00716.56
7.4.250.0000.00816.49
7.4.240.0030.00516.57
7.4.230.0000.00816.51
7.4.220.0100.01716.36
7.4.210.0070.00916.55
7.4.200.0030.00316.40
7.4.190.0000.00716.54
7.4.160.0060.00916.43
7.4.150.0090.00917.40
7.4.140.0070.01517.86
7.4.130.0030.01516.47
7.4.120.0140.00516.46
7.4.110.0110.00916.52
7.4.100.0090.01816.37
7.4.90.0110.00616.32
7.4.80.0090.00916.54
7.4.70.0040.01316.41
7.4.60.0000.01716.58
7.4.50.0060.00316.27
7.4.40.0040.01122.77
7.4.30.0070.01016.66
7.4.00.0050.01215.04
7.3.330.0000.00513.28
7.3.320.0000.00513.29
7.3.310.0040.00416.25
7.3.300.0050.00316.13
7.3.290.0040.01116.22
7.3.280.0050.01416.23
7.3.270.0120.01217.40
7.3.260.0100.00716.57
7.3.250.0100.00816.39
7.3.240.0070.01016.41
7.3.230.0120.00616.60
7.3.210.0080.00916.21
7.3.200.0100.01019.39
7.3.190.0060.01016.35
7.3.180.0060.01016.38
7.3.170.0060.01316.27
7.3.160.0030.01316.29
7.3.120.0020.01314.72
7.3.110.0100.00614.70
7.3.100.0060.01014.98
7.3.90.0030.00714.70
7.3.80.0060.00315.10
7.3.70.0120.00314.94
7.3.60.0060.00614.75
7.3.50.0060.01014.70
7.3.40.0030.01014.79
7.3.30.0030.00614.66
7.3.20.0060.00916.18
7.3.10.0050.00816.52
7.3.00.0080.00716.23
7.2.330.0090.00916.33
7.2.320.0180.00716.55
7.2.310.0130.00316.61
7.2.300.0040.01416.48
7.2.290.0100.00916.52
7.2.250.0040.01414.97
7.2.240.0000.01714.87
7.2.230.0060.00614.97
7.2.220.0060.00615.13
7.2.210.0000.01114.84
7.2.200.0070.00714.98
7.2.190.0000.01014.77
7.2.180.0070.00314.98
7.2.170.0040.00814.78
7.2.130.0030.01216.43
7.2.120.0030.01316.60
7.2.110.0000.01516.74
7.2.100.0040.00916.68
7.2.90.0070.01116.84
7.2.80.0030.01416.73
7.2.70.0000.01016.68
7.2.60.0070.01016.52
7.2.50.0030.01316.79
7.2.40.0060.00916.82
7.2.30.0000.01516.67
7.2.20.0060.00816.81
7.2.10.0030.01316.66
7.2.00.0060.00617.97
7.1.330.0000.01515.63
7.1.320.0030.01015.69
7.1.310.0030.01015.54
7.1.300.0000.01115.52
7.1.290.0070.00715.65
7.1.280.0120.00615.42
7.1.270.0040.01115.61
7.1.260.0070.00715.64
7.1.250.0070.00715.65
7.1.240.0040.01015.67
7.1.230.0060.01115.59
7.1.220.0030.00715.61
7.1.210.0060.00915.56
7.1.200.0000.01715.61
7.1.190.0030.01015.51
7.1.180.0090.00615.37
7.1.170.0000.01315.48
7.1.160.0090.00615.37
7.1.150.0030.01015.39
7.1.140.0070.00315.58
7.1.130.0060.00615.38
7.1.120.0040.00815.69
7.1.110.0050.00515.59
7.1.100.0020.01016.64
7.1.90.0090.00615.54
7.1.80.0090.00615.63
7.1.70.0060.00316.03
7.1.60.0030.01417.29
7.1.50.0050.01216.10
7.1.40.0050.00815.68
7.1.30.0000.01415.64
7.1.20.0060.00915.66
7.1.10.0000.01315.48
7.1.00.0050.04318.84
7.0.330.0000.01415.21
7.0.320.0040.01115.04
7.0.310.0080.00315.28
7.0.300.0070.00715.32
7.0.290.0110.00315.22
7.0.280.0000.00915.35
7.0.270.0060.00615.01
7.0.260.0140.00015.30
7.0.250.0050.00515.46
7.0.240.0040.00714.93
7.0.230.0120.00015.45
7.0.220.0060.00615.43
7.0.210.0040.01115.23
7.0.200.0120.00315.90
7.0.190.0030.00615.30
7.0.180.0060.00615.34
7.0.170.0030.00615.23
7.0.160.0030.00615.45
7.0.150.0040.01115.25
7.0.140.0070.02718.71
7.0.130.0030.01015.19
7.0.120.0070.00715.34
7.0.110.0060.00615.25
7.0.100.0110.03017.70
7.0.90.0070.04217.62
7.0.80.0100.03617.62
7.0.70.0080.04417.57
7.0.60.0100.02317.46
7.0.50.0100.03317.84
7.0.40.0050.02516.83
7.0.30.0020.03816.56
7.0.20.0100.03516.75
7.0.10.0030.03816.70
7.0.00.0050.03716.56
5.6.380.0030.01013.66
5.6.370.0060.00614.00
5.6.360.0030.00614.13
5.6.350.0060.00314.21
5.6.340.0130.00314.37
5.6.330.0030.01014.16
5.6.320.0070.01014.43
5.6.310.0040.01113.98
5.6.300.0090.00014.11
5.6.290.0100.00014.20
5.6.280.0000.04517.42
5.6.270.0100.00714.13
5.6.260.0030.00613.92
5.6.250.0070.04417.57
5.6.240.0050.02717.43
5.6.230.0070.04317.33
5.6.220.0080.04317.44
5.6.210.0080.04117.49
5.6.200.0050.03817.68
5.6.190.0050.04217.75
5.6.180.0100.02717.64
5.6.170.0030.02717.63
5.6.160.0050.02417.66
5.6.150.0100.04017.39
5.6.140.0030.03517.65
5.6.130.0050.03717.55
5.6.120.0050.03817.53
5.6.110.0080.03517.66
5.6.100.0100.02717.72
5.6.90.0050.04217.40
5.6.80.0090.04417.13
5.6.70.0090.03117.19
5.6.60.0040.02917.27
5.6.50.0050.03317.28
5.6.40.0050.03017.21
5.6.30.0070.04017.05
5.6.20.0030.03517.19
5.6.10.0040.02317.14
5.6.00.0070.03617.18
5.5.380.0050.03715.73
5.5.370.0100.03515.82
5.5.360.0030.02515.71
5.5.350.0080.04315.74
5.5.340.0080.02115.90
5.5.330.0050.02215.92
5.5.320.0040.04016.08
5.5.310.0030.02915.83
5.5.300.0070.04315.82
5.5.290.0000.02515.83
5.5.280.0050.04415.82
5.5.270.0050.03315.97
5.5.260.0060.04215.86
5.5.250.0050.03015.74
5.5.240.0040.02515.51
5.5.230.0020.02415.55
5.5.220.0070.04115.40
5.5.210.0000.02715.60
5.5.200.0020.02515.51
5.5.190.0070.04115.58
5.5.180.0070.03615.53
5.5.170.0040.00410.93
5.5.160.0050.04015.60
5.5.150.0060.02115.46
5.5.140.0070.02815.37
5.5.130.0090.01815.53
5.5.120.0070.04415.63
5.5.110.0050.04115.31
5.5.100.0050.02215.25
5.5.90.0050.03715.62
5.5.80.0060.03515.59
5.5.70.0070.02215.50
5.5.60.0070.03915.62
5.5.50.0030.03815.43
5.5.40.0080.04015.55
5.5.30.0020.03215.34
5.5.20.0070.03115.40
5.5.10.0020.04915.35
5.5.00.0080.03515.39
5.4.450.0100.04215.16
5.4.440.0040.02915.09
5.4.430.0080.03815.06
5.4.420.0110.03915.15
5.4.410.0030.02315.02
5.4.400.0060.02214.82
5.4.390.0020.03214.92
5.4.380.0020.02414.88
5.4.370.0020.02715.05
5.4.360.0030.02114.98
5.4.350.0080.03014.82
5.4.340.0020.02314.86
5.4.330.0060.00310.59
5.4.320.0070.02314.78
5.4.310.0040.02314.75
5.4.300.0030.02314.79
5.4.290.0030.02314.99
5.4.280.0050.02314.70
5.4.270.0070.04014.85
5.4.260.0050.03014.88
5.4.250.0080.02414.66
5.4.240.0040.03415.02
5.4.230.0050.03014.95
5.4.220.0030.02914.87
5.4.210.0050.02314.93
5.4.200.0090.03514.85
5.4.190.0090.01514.77
5.4.180.0040.03714.93
5.4.170.0070.04114.78
5.4.160.0030.03814.92
5.4.150.0030.03914.92
5.4.140.0060.02113.71
5.4.130.0090.03013.71
5.4.120.0030.02113.50
5.4.110.0050.02013.68
5.4.100.0030.02213.71
5.4.90.0050.02313.69
5.4.80.0000.02913.58
5.4.70.0070.01713.60
5.4.60.0050.03613.62
5.4.50.0100.03313.66
5.4.40.0050.04013.63
5.4.30.0030.02313.53
5.4.20.0000.02213.70
5.4.10.0050.01813.68
5.4.00.0070.01513.33
5.3.290.0020.04612.61
5.3.280.0040.03412.63
5.3.270.0080.02812.59
5.3.260.0060.03012.57
5.3.250.0070.03812.58
5.3.240.0050.01812.63
5.3.230.0050.02312.59
5.3.220.0050.01812.57
5.3.210.0020.03112.63
5.3.200.0030.03312.59
5.3.190.0030.02212.58
5.3.180.0030.04012.53
5.3.170.0030.01912.65
5.3.160.0050.02012.60
5.3.150.0080.02012.54
5.3.140.0030.04212.53
5.3.130.0050.02112.59
5.3.120.0020.02712.60
5.3.110.0060.01912.60
5.3.100.0050.02812.28
5.3.90.0050.03312.25
5.3.80.0040.02812.32
5.3.70.0000.02512.28
5.3.60.0030.04212.34
5.3.50.0080.02012.31
5.3.40.0060.04212.26
5.3.30.0070.03912.29
5.3.20.0060.03112.13
5.3.10.0050.01812.06
5.3.00.0020.04012.06

preferences:
38.4 ms | 401 KiB | 5 Q