/** * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license */ /* globals document, setTimeout */ document.getElementById( 'run' ).addEventListener( 'click', () => { log( 'Running tests...' ); setTimeout( async () => { await runTest( 'concat', testConcat ); await runTest( 'spread operator', testSpread ); await runTest( 'for-loop', testForLoop ); await runTest( 'ultra-loop', testUltraForLoop ); log( 'done' ); } ); } ); const output = document.getElementById( 'output' ); function log( line ) { const paragraphElement = document.createElement( 'p' ); paragraphElement.innerText = line; output.appendChild( paragraphElement ); } function runTest( name, callback ) { return new Promise( resolve => { const start = new Date(); const repetitions = 10000000; const z = Array( repetitions ); const root = { root: 'foo', path: [ 0 ] }; const path = [ 0, 2 ]; for ( let i = 0; i < repetitions; i++ ) { const newPath = callback( root, path ); z[ i ] = [ newPath.length ]; } const end = new Date(); log( ` > ${ name } took ${ end - start }ms` ); setTimeout( () => { resolve(); }, 50 ); } ); } class Position { constructor( root, path, stickiness = 'left', pathMergeMethod ) { if ( !( path instanceof Array ) || path.length === 0 ) { throw new Error( 'model-position-path-incorrect-format' ); } path = pathMergeMethod( root.path, path ); root = root.root; this.root = root; this.path = path; this.stickiness = stickiness; } } function testConcat( root, path ) { return new Position( root, path, 'right', concat ); } function testSpread( root, path ) { return new Position( root, path, 'right', spread ); } function testForLoop( root, path ) { return new Position( root, path, 'right', forLoop ); } function testUltraForLoop( root, path ) { return new Position( root, path, 'right', ultraForLoop ); } function concat( rootPath, path ) { return rootPath.concat( path ); } function spread( rootPath, path ) { return [ ...rootPath, ...path ]; } function forLoop( rootPath, path ) { const newPath = []; for ( let i = 0; i < rootPath.length; i++ ) { newPath.push( rootPath[ i ] ); } for ( let i = 0; i < path.length; i++ ) { newPath.push( path[ i ] ); } return newPath; } function ultraForLoop( rootPath, path ) { const fullLength = rootPath.length + path.length; const newPath = new Array( fullLength ); for ( let i = 0; i < rootPath.length; i++ ) { newPath[ i ] = rootPath[ i ]; } for ( let i = 0; i < path.length; i++ ) { newPath[ rootPath.length + i ] = path[ i ]; } return newPath; }