Skip to content

Instantly share code, notes, and snippets.

@tobiasWenger
Last active February 21, 2017 12:13
Show Gist options
  • Select an option

  • Save tobiasWenger/5ed7c167f51d1532dcc08ddf0b271771 to your computer and use it in GitHub Desktop.

Select an option

Save tobiasWenger/5ed7c167f51d1532dcc08ddf0b271771 to your computer and use it in GitHub Desktop.
computed.sort vs. sortBy
import Ember from 'ember';
import Performance from '../performance';
export default Ember.Component.extend(Performance, {
dataSorting: ['string'],
dataSorted: Ember.computed.sort('data', 'dataSorting')
});
import Ember from 'ember';
import Performance from '../performance';
export default Ember.Component.extend(Performance, {
dataSorted: Ember.computed('data', function() {
return this.get('data').sortBy('string');
})
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
data1: Ember.computed(function() {
// create array with id and random string
let data = [];
for (let i = 0; i < 2000; i++) {
const randomString = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5);
data.push({id: i, string: randomString});
}
return data;
}),
data2: Ember.computed(function() {
// create array with id and random string
let data = [];
for (let i = 0; i < 2000; i++) {
const randomString = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5);
data.push({id: i, string: randomString});
}
return data;
})
});
import Ember from 'ember';
export default Ember.Mixin.create({
tStart: 0,
tEnd: 0,
deltaT: Ember.computed('tStart', 'tEnd', function() {
const { tStart, tEnd } = this.getProperties('tStart', 'tEnd');
return Math.round((tEnd - tStart)*100)/100;
}),
init() {
this.set('tStart', performance.now());
this._super(...arguments);
},
didInsertElement() {
this._super(...arguments);
this.set('tEnd', performance.now());
}
})
<h1>computed.sort vs. sortBy</h1>
<br>
<h2>computed.sort</h2>
{{computed-sort data=data2}}
<br>
<h2>sortBy</h2>
{{sort-by data=data1}}
sort time: <b>{{deltaT}} ms</b> for {{dataSorted.length}} items
sort time: <b>{{deltaT}} ms</b> for {{dataSorted.length}} items
{
"version": "0.11.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.10.2",
"ember-data": "2.11.0",
"ember-template-compiler": "2.10.2",
"ember-testing": "2.10.2"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment