PowerShell 中使用json对象的性能比较
PowerShell v3 – Creating Objects With [pscustomobject] – it’s fast!
*****Warning. This is from a preview release******
PowerShell v2 brought the ability to create a custom object via the following method:
1 |
$CustomObject1 = New-Object psobject -Property @{a=1; b=2; c=3; d=4} |
2 |
3 |
$CustomObject1 | Format-List |

PowerShell v3 brings the possibility to create a custom object via
[pscustomobject]
1 |
$CustomObject2 = [pscustomobject]@{a=1; b=2; c=3; d=4} |
2 |
3 |
$CustomObject2 | Format-List |

Note: both methods create a PSCustomObject with NoteProperties, not a hashtable object
1 |
$CustomObject1 | Get-Member |
2 |
3 |
$CustomObject2 | Get-Member |

So, why would you want to do it this way? Well firstly it preserves the insertion order,which helps with my OCD issues again. However, the main reason I have seen so far is that it is also a lot quicker. Fellow PowerShell MVP Tome Tanasovski carried out some basic performance testing which I thought I would highlight here.
There are four different ways you can create a custom object and a typical use case would be using PowerShell for reporting purposes, e.g. iterating through a list of VMs and pulling out various properties of them to create a report. With a very basic example, let’s have a look at the speed differences:
1) Select-Object
Not everybody knows that it’s possible to create a custom object with Select-Object. This was a handy trick since v1 days and was pretty quick too.
1 |
$TestSelect = { |
2 |
(0..5000) | ForEach-Object {$CustomObject = "" | Select-Object Name,ID |
3 |
$CustomObject.Name = "Test Name" |
4 |
$CustomObject.ID = $_ |
5 |
$CustomObject |
6 |
} |
7 |
} |
8 |
Measure-Command $TestSelect | Format-Table TotalSeconds -Autosize |

2) Add-Member
1 |
$TestAddMember = { |
2 |
(0..5000) | ForEach-Object {$CustomObject = New-Objectpsobject |
3 |
$CustomObject | Add-Member -Name "Name" -Value "Test Name" |
4 |
$CustomObject | Add-Member -Name "ID" -Value $_ |
5 |
$CustomObject |
6 |
} |
7 |
} |
8 |
Measure-Command $TestAddMember | Format-Table TotalSeconds -Autosize |

3) Property Parameter
1 |
$TestProperty = { |
2 |
(0..5000) | ForEach-Object {New-Object psobject -Property@{Name = "Test Name"; ID = $_}} |
3 |
} |
4 |
Measure-Command $TestProperty | Format-Table TotalSeconds -Autosize |
4) [pscustomobject]
1 |
$TestProperty = { |
2 |
(0..5000) | ForEach-Object {[pscustomobject]@{Name = "Test Name"; ID = $_}} |
3 |
} |
4 |
Measure-Command $TestPSCustomObject | Format-TableTotalSeconds -Autosize |

So a summary of the these basic testing results looks pretty good for [pscustomobject]!
Select-Object = 7.74s
Add-Member = 28.87s
Property = 7.29
[pscustomobject] = 0.94s
I hope to try out [pscustomobject] on some of my reporting scripts and see what difference it makes to real world testing.
PowerShell 中使用json对象的性能比较的更多相关文章
- MVC中处理Json和JS中处理Json对象
MVC中处理Json和JS中处理Json对象 ASP.NET MVC 很好的封装了Json,本文介绍MVC中处理Json和JS中处理Json对象,并提供详细的示例代码供参考. MVC中已经很好的封装了 ...
- js中的json对象详细介绍
JSON一种简单的数据格式,比xml更轻巧,在JavaScript中处理JSON数据不需要任何特殊的API或工具包,下面为大家详细介绍下js中的json对象, 1.JSON(JavaScript Ob ...
- 简单使用JSON,JavaScript中创建 JSON 对象(一)
JSON:JavaScript 对象表示法(JavaScript Object Notation). JSON 是存储和交换文本信息的语法.类似 XML. JSON 比 XML 更小.更快,更易解析. ...
- js中 给json对象添加属性和json数组添加元素
js中 给json对象添加新的属性 比如现在有一个json对象为jsonObj,需要给这个对象添加新的属性newParam,同时给newParam赋值为pre.做法如下: var obj={ &quo ...
- 利用reduce方法,对数组中的json对象去重
数组中的json对象去重 var arr = [{ "name": "ZYTX", "age": "Y13xG_4wQnOWK1Q ...
- js中解析json对象:JSON.parse()用于从一个字符串中解析出json对象, JSON.stringify()用于从一个对象解析出字符串。
JSON.parse()用于从一个字符串中解析出json对象. var str = '{"name":"huangxiaojian","age&quo ...
- java中的JSON对象的使用
申明:没工作之前都没听过JSON,可能是自己太菜了.可能在前台AJAX接触到JSON,这几天要求在纯java的编程中,返回JSON字符串形式. 网上有两种解析JSON对象的jar包:JSON-lib. ...
- js中的json对象
1.JSON(JavaScript Object Notation)一种简单的数据格式,比xml更轻巧.JSON是JavaScript原生格式,这意味着在JavaScript中处理JSON数据不须要 ...
- ios中解析json对象基类
这个是对上面一篇写的一个解析json对象的基类 @interface BaseObjectFromJson : NSObject + (id) objectWithDict:(NSDictionary ...
随机推荐
- ASP.NET获取IP的6种方法(转载于LanceZhang's Tech Blog)
服务端: //方法一 HttpContext.Current.Request.UserHostAddress; //方法二 HttpContext.Current.Request.ServerVari ...
- my dup2
#include <fcntl.h>#include <stdio.h>#include <unistd.h> int mydup(int i_OldFd, int ...
- 取得select框的text
function selectInput(choose) { alert(choose.options[choose.selectedIndex].text); }
- 【架构师之路】依赖注入原理---IoC框架
1 IoC理论的背景 我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑. 图1:软件系统中耦合的对象 如果我们 ...
- 题目:[NOIP1999]拦截导弹(最长非递增子序列DP) O(n^2)和O(n*log(n))的两种做法
题目:[NOIP1999]拦截导弹 问题编号:217 题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发 ...
- sql列转行
1.需要实现一个单行的统计报表 思路先用一个union查出单列,然后再把单列转成单行 2.实现 SELECT MAX(CASE WHEN type = 1 THEN num ELSE 0 END) A ...
- Android应用调试经常使用知识
1.Android应用启动过程调试 1).进入设置-->辅助功能-->开发人员选项:假设没有打开开发人员模式.在拨号里面输入*#*#6961#*#*: 2).找到选择调试应用,打开选择你要 ...
- Java基础知识强化65:基本类型包装类之Integer的构造方法
1. Integer类概述 (1)Integer类在对象中包装了一个基本类型 int 的值,Integer类型的对象包含一个int类型的字段. (2)该类提供了多个方法,能在int类型和String类 ...
- html.css溢出
<!DOCTYPE html><!DOCTYPE html><html><head> <title></title> <m ...
- (一)Knockout - 入门
knockout 简介 knockoutjs的实现依照[MVVM模式],Model-View-ViewModel. Model,用来聚合server端数据 ViewModel,描述的数据以及操作,是行 ...